×
Get Full Access to Discrete Mathematics And Its Applications - 7 Edition - Chapter 3.1 - Problem 37e
Get Full Access to Discrete Mathematics And Its Applications - 7 Edition - Chapter 3.1 - Problem 37e

×

# Adapt the bubble sort algorithm so that it stops when no

ISBN: 9780073383095 37

## Solution for problem 37E Chapter 3.1

Discrete Mathematics and Its Applications | 7th Edition

• Textbook Solutions
• 2901 Step-by-step solutions solved by professors and subject experts
• Get 24/7 help from StudySoup virtual teaching assistants

Discrete Mathematics and Its Applications | 7th Edition

4 5 1 430 Reviews
18
3
Problem 37E

Adapt the bubble sort algorithm so that it stops when no interchanges are required. Express this more efficient version of the algorithm in pseudocode.

Step-by-Step Solution:

SolutionStep 1In this problem, we have to adapt that if interchange is not required then it stop. Write the algorithm of pseudo code in an efficient way of bubble sort.Bubble_sort(num1, num2, num3, ……….. Set of integers)Now, start the loopfor(index = 0 ; index < size ; index++) where index be the position in a list Assign the boolean value bool = false (Boolean variable is required if interchange is not required it stop) for(index_2 = 0 ; index_2 < size - index_1-1 ; index_2++) if(element at index position > element at index_2 position) then Bool = true (because the condition is true and interchange required) Assign the new temporary variable temp = element at index_2+1 position element at index_2 + 1 position = element at index_2 position element at index_2 position = temp End of if condition End of the index_2 loop if (not flag) (if no interchange require it stop) return End of the main loopBest case of complexity will be O(n).

Step 2 of 3

Step 3 of 3

#### Related chapters

Unlock Textbook Solution