1 / 10

Analysis of Bubble Sort and Loop Invariant

Analysis of Bubble Sort and Loop Invariant. 1 2 3 4 5 6. 77. 5. 101. 42. 35. 12. 1 2 3 4 5 6. 5. 77. 101. 35. 12. 42. 1 2 3 4 5 6.

bozica
Download Presentation

Analysis of Bubble Sort and Loop Invariant

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Analysis of Bubble Sort and Loop Invariant

  2. 1 2 3 4 5 6 77 5 101 42 35 12 1 2 3 4 5 6 5 77 101 35 12 42 1 2 3 4 5 6 42 77 101 12 35 5 N - 1 1 2 3 4 5 6 42 77 101 12 5 35 1 2 3 4 5 6 42 77 101 5 12 35 N-1 Iteration

  3. To do N-1 iterations procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1 loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloop endprocedure // Bubblesort To bubble a value Outer loop Inner loop

  4. Bubble Sort Time Complexity • Best-Case Time Complexity • The scenario under which the algorithm will do the least amount of work (finish the fastest) • Worst-Case Time Complexity • The scenario under which the algorithm will do the largest amount of work (finish the slowest)

  5. Bubble Sort Time Complexity Called Linear Time O(N) Order-of-N • Best-Case Time Complexity • Array is already sorted • Need 1 iteration with (N-1) comparisons • Worst-Case Time Complexity • Need N-1 iterations • (N-1) + (N-2) + (N-3) + …. + (1) = (N-1)* N / 2 Called Quadratic Time O(N2) Order-of-N-square

  6. Loop Invariant • It is a condition or property that is guaranteed to be correct with each iteration in the loop • Usually used to prove the correctness of the algorithm

  7. Loop Invariant for Bubble Sort • By the end of iteration i the right-most i items (largest) are sorted and in place

  8. 1 2 3 4 5 6 77 5 101 42 35 12 1 2 3 4 5 6 5 77 101 35 12 42 1 2 3 4 5 6 42 77 101 12 35 5 N - 1 1 2 3 4 5 6 42 77 101 12 5 35 1 2 3 4 5 6 42 77 101 5 12 35 N-1 Iterations 1st 2nd 3rd 4th 5th

  9. Correctness of Bubble Sort (using Loop Invariant) • Bubble sort has N-1 Iterations • Invariant: By the end of iteration i the right-most i items (largest) are sorted and in place • Then: After the N-1 iterations  The right-most N-1 items are sorted • This implies that all the N items are sorted

  10. Correctness of Bubble Sort (using Loop Invariant) • What if we stop early (after iteration K) • Remember the Boolean “did_swap” flag • From the invariant  the right-most K items are sorted • A[N-(K-1)] < A[N-2] < A[N-1] < A[N] • Since we stopped early, then no swaps are done, Then • A[1] < A[2] < ….. < A[N-K] • By merging these two, then the entire array is sorted

More Related