Question

# 19.5 (Enhanced Bubble Sort) Explain why bubble sort is an O(n^2) algorithm.

19.5 (Enhanced Bubble Sort) Explain why bubble sort is an O(n^2) algorithm.

```void bubbleSort(int arr[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

This bubble sort code has a nested for loop

First for loop running for i values from 0 to n-1
So, Number of values for i = n-1

Inner for loop running for j values from 0 to n-i-1
So, in worst case inner for loop running for n-1 times

Total time complexity
= O((n-1)(n-1))
= O(n^2)
```

#### Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
• ### Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

• ### Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate...

Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate the expected time complexity of the algorithm using the big-O notation. Use the following format for an algorithm pseudocode Function header (.....) Input: Output: Algorithm steps: 1. 2.

• ### a) Use the bubble sort to sort g, j, o, n, d, f showing the lists...

a) Use the bubble sort to sort g, j, o, n, d, f showing the lists obtained at each step. b) Show that the function f(n) = (n + 3)(5n – 1) + 13 is O(n?).

• ### 2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running...

2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running time analysis for both best and worst case. 3. Consider the age data of 12 children who are supposed to undergo for vaccination in ascending order of their age. Suggest and apply a sorting technique which can efficiently handle this data. Show all the intermediate steps clearly. Child ID 01 02 03 04 05 06 07 08 09 10 11 12 2. Age 1...

• ### Implement a parallel version of a sorting algorithm (bubble sort or merge sort). Done in java...

Implement a parallel version of a sorting algorithm (bubble sort or merge sort). Done in java and a minimum of two threads utilised.

• ### in c++ Sort the following list using the bubble sort algorithm as discussed in this chapter....

in c++ Sort the following list using the bubble sort algorithm as discussed in this chapter. Show the list after each iteration of the outer for loop.2) 82, 17, 40, 28, 15, 55, 46, 93, 6, 67, 11, 3 6.

• ### Sort the following list using the bubble sort algorithm as discussed in this chapter. Show the...

Sort the following list using the bubble sort algorithm as discussed in this chapter. Show the list after each iteration of the outer for loop. 46, 58, 16, 25, 83, 98, 8, 70, 5, 62

• ### Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

• ### 07-15 pts) Develop a recursive version of the Bubble Sort algorithm. (a) Write the pseudo code...

07-15 pts) Develop a recursive version of the Bubble Sort algorithm. (a) Write the pseudo code of the algorithm and justify that it is recursive and works correctly Write the recurrence relation for the algorithm and solve it using one of the two approaches discussed in class, as appropriate. Solve the recurrence relation and show that the time complexity of the recursive algorithm is θ(n).

• ### Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...