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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started