analysis of bubble sort and loop invariant. n-1 iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77...

10
Analysis of Bubble Sort and Loop Invariant

Upload: claud-hunter

Post on 04-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

Analysis of Bubble Sort and Loop Invariant

Page 2: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

N-1 Iteration

77123542 51 2 3 4 5 6

101

5421235 771 2 3 4 5 6

101

42 5 3512 771 2 3 4 5 6

101

42 35 512 771 2 3 4 5 6

101

42 35 12 5 771 2 3 4 5 6

101

N -

1

Page 3: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

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 endloopendprocedure // Bubblesort

Inn

er l

oo

p

Ou

ter

loo

p

To bubble a value

To do N-1 iterations

Page 4: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

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)

Page 5: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

Bubble Sort Time Complexity

• 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 Linear Time O(N) Order-of-N

Called Quadratic Time O(N2) Order-of-N-square

Page 6: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

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

Page 7: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

Loop Invariant for Bubble Sort

• By the end of iteration i the right-most i items (largest) are sorted and in place

Page 8: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

N-1 Iterations

1st

2nd

3rd

4th

77123542 51 2 3 4 5 6

101

5421235 771 2 3 4 5 6

101

42 5 3512 771 2 3 4 5 6

101

42 35 512 771 2 3 4 5 6

101

42 35 12 5 771 2 3 4 5 6

101

N -

1

5th

Page 9: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

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

Page 10: Analysis of Bubble Sort and Loop Invariant. N-1 Iteration 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35

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