What is the worst case running time for BS when given a vector of size N to sort?

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section7.5: Case Studies
Problem 3E
icon
Related questions
Question

Given BS in the picture.

A) What is the worst case running time for BS when given a vector of
size N to sort?

B) Put a series of integers into this vector that would make the BS run in optimal time:

         

C) Put a series of integers into this vector that would make the BS run in worst case time:

         
### Bubble Sort Implementation in C++

The following code demonstrates a simple implementation of the Bubble Sort algorithm in C++. The function `BS` takes a vector of integers and sorts it in ascending order.

```cpp
void BS(vector<int> & list) {
    int clean_pass = false;
    for (int i = 1; !clean_pass && i < list.size(); i++) {
        clean_pass = true;
        for(int j = 0; j < (list.size() - i); j++) {
            if(list[j] > list[j+1]) {  // Swap j & j+1
                int temp = list[j];
                list[j] = list[j+1];
                list[j+1] = temp;
                clean_pass = false;
            }
        }
    }
}
```

### Explanation

- **Outer Loop**: Iterates through the list. It continues until a complete pass through the list is made without any swaps, indicating the list is sorted.
  
- **Inner Loop**: Compares each element with the next one. If an element is greater than the next, they are swapped to move larger elements towards the end of the list.

- **Variables**:
  - `clean_pass`: A flag used to track whether the inner loop made any swaps.
  - `temp`: A temporary variable used for swapping two elements.

This implementation uses an optimization to improve efficiency by stopping the sort early if a pass is made without any swaps (`clean_pass = true`). This means the list is already sorted.
Transcribed Image Text:### Bubble Sort Implementation in C++ The following code demonstrates a simple implementation of the Bubble Sort algorithm in C++. The function `BS` takes a vector of integers and sorts it in ascending order. ```cpp void BS(vector<int> & list) { int clean_pass = false; for (int i = 1; !clean_pass && i < list.size(); i++) { clean_pass = true; for(int j = 0; j < (list.size() - i); j++) { if(list[j] > list[j+1]) { // Swap j & j+1 int temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; clean_pass = false; } } } } ``` ### Explanation - **Outer Loop**: Iterates through the list. It continues until a complete pass through the list is made without any swaps, indicating the list is sorted. - **Inner Loop**: Compares each element with the next one. If an element is greater than the next, they are swapped to move larger elements towards the end of the list. - **Variables**: - `clean_pass`: A flag used to track whether the inner loop made any swaps. - `temp`: A temporary variable used for swapping two elements. This implementation uses an optimization to improve efficiency by stopping the sort early if a pass is made without any swaps (`clean_pass = true`). This means the list is already sorted.
Expert Solution
Step 1

From the inner for loop, it is clear that if the current element is greater than the next element of vector swap the elements, which means to sort the vectors in ascending order.

 

The worst case running time for any block is calculated when all the set of instructions were executed in every iteration.

Thus if block of code must execute in every iteration.

Thus, worst case running time for block BS when size of vector is N is:  O(N2 ) as both the for loops executes in every iteration.

 

 

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Polynomial time
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage