Question: Bubble sort with an array A[1..n] of length n. The array has the values 1, 2, ..., n, that are randomly permuted so that each
Bubble sort with an array A[1..n] of length n. The array has the values 1, 2, ..., n, that are randomly permuted so that each permutation is equally likely.
BubbleSort(A)
for i = 1 to n
for j = n downto i+1
if A[j] < A[j-1]
swap A[j] with A[j-1]
BubbleSort will sort the array in increasing order.
What is the expected number of swaps in the algorithm, and explain your derivation? Note that your expected number is an exact formula dependent on n, i.e., its not in Big-O form. (Hint: You can use indicator functions to determine the expected number of swaps, similar to how they were used to analyze Quicksort.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
