CS312 10/17/2006 CLASS NOTES Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. Any sorting algorithm has an array of n integers as input and output is sorted where number on the left is always less than the number on right. A[1…n]-- A[1….n] where n=length[A] Such that A [i] A [i+1] i n-1 Bubble sort The bubble sort algorithm starts at the beginning of the data set. It compares the first two elements, and if the first is greater than the second, it swaps them. It continues doing this for each pair of adjacent elements to the end of the data set. It then starts again with the first two elements, repeating until no swaps have occurred on the last pass. Bubble Sort (int [] A){ for (i = 1…. n ){ for ( j = 1…. n){ if (A [i]> A [j]){ Swap (i, j); } } } } Swap ( i , j ){ temp = i; i=j; j = temp; } Running time of Bubble sort can be n2 But it is not always n every time so actually less than n2 Calculation of running time of Bubble Sort: for ( i = 1…. n ){ running time for outer for loop = n for ( j = 1…. n ){ running time for inner for loop = n but it is not always n every time When i = 1 n-1 1 CS312 10/17/2006 CLASS NOTES When i = 2 n-2 When i = 3 n-3 and so on (n-1) +(n-2) +(n-3) ……1 + 0 =∑n-1 j (Sum of j where j goes from 0 to n-1) j=0 If is same no of times as inner loop =∑n-1 j 1. j=0 2. Swap may execute or not: Worst case: execute every time so = tj ∑n-1 j j=0 tj is going to be 0 or 1 based on whether or not we get into it. Now sum all the steps is overall running time T (n)= n + ∑n-1 j + ∑n-1 j + ∑n-1 j + tj ∑n-1 j j=0 j=0 j=0 j=0 Recall Arithmetic series formula: ∑n i= [n (n+1)/2] i=0 ∑ n-1 i = i=0 ∑ n I -n i=0 = = = [(n2+n)/2] – n [(n2+n)/2] – 2n/2 [(n2-n)/2] = [n (n-1)/2] And worst case tj is always 1 so tj ∑n-1 j ∑n-1 i = j=0 i=0 T (n)= n + [n (n-1)/2] + [n (n-1)/2] + [n (n-1)/2] T (n)= n + [1/2] n2- [1/2] n/2] + [1/2] n2- [1/2] n/2] + [1/2] n2- [1/2] n/2] n2 - [1/2] n T (n)=O (n2) T (n)=[3/2] And Best case tj is always 0 so tj ∑n-1 j = 0 j=0 T (n) = n + [n (n-1)/2] + [n (n-1)/2] + 0 T (n) = n + [1/2] n2- [1/2] n/2] + [1/2] n2- [1/2] n/2] 2 T (n) = n T (n)=O (n2) If best case and worst case is same then it is also big theta (n2) 2 CS312 10/17/2006 CLASS NOTES Proofs Proof of program correctness using : Proof by Induction Proof by loop Invariant Induction A Proof by Induction has 3 steps 1. The base case 2. Assumption 3. Induction The Base case: Verify the Base Case. Usually this involves checking that a statement is true at n=0 and n=1. What you are trying to do is prove that the smallest case is true. If the smallest case is false, then the whole proposition will be false. Assumption: Formulate the Inductive Hypothesis and state that you are assuming the hypothesis to be true for some specific integer k. Induction: Here you will prove that the Inductive Hypothesis is true for the next case. If you start with k in your assumption, prove k + 1 is true. Substitute the inductive hypothesis and show that the proposition is still true at k+1. So use base Case & assumption to prove induction. We can conclude that since the base case is true and the inductive step is true, proposition is true for all n. Proof by Loop Invariant: Loop invariant is simply some condition that is always true just before loop iteration executes (including the first iteration) and just after a loop iteration (including the last iteration). Loop invariants can help you both design and prove correct your iterative procedures. If we can prove that a loop invariant is true each time we pass it while executing a loop, we have proven the correctness of the loop. A Proof by Loop Invariant also has 3 steps 1. Initialization – same as Base case 2. Maintenance – same as assumption 3. Termination – same as Induction It’s a special case of Proof by Induction and once we know what the loop invariant of a program is, we can prove the correctness of the loop with respect to the loop invariant using induction. The process is: Initialization: Prove that the loop invariant is true after the first iteration (or if the loop does not iterate at all, prove the loop invariant is true after 0 iterations). Maintenance: Assume the loop invariant is true after K iterations, and then prove that it must also be true after K+1 iterations. 3 CS312 10/17/2006 CLASS NOTES Termination – is true after nth iteration. Example: For bubble sort: figure out what is true after every iteration of array (10,8,6,9) I=1 J=2 J=3 J=4 10 8 6 6 8 10 10 10 6 6 8 8 9 9 9 9 I=2 J=3 J=4 6 6 6 10 8 8 8 10 10 9 9 9 I=3 J=4 6 6 8 8 10 9 9 9 For i=1 1 element sorted For i=2 2 elements sorted For i=3 3 elements sorted After ith iteration we end up with i smallest no of sorted elements. Loop Invariant: Before the ith iteration of the for loop we know that i-1 smallest elements of array A [1…. n] will be sorted in the subarray of A [1…. i-1] Initialization: Before the 1st iteration of the for- loop we know that 0(1-1) smallest elements of array A [1…. n] will be sorted in the subarray of A [1…. 0] so if loop does not iterate loop variant is true. Our proposition for bubble sort is to find smallest element from A [i] to A [n] & we’re going to put it in A [i]. Assume that A [i] is the value of the 1st element. Inner for-loop is going to find smallest element in remaining subarray of A [i….n] and we place it into A[i] such that A [i] to A [n] contains i smallest elements. Maintenance: Assume the loop invariant is true after K iterations, and then prove that it must also be true after K+1 iterations. Termination: One way to prove loop termination is to identify an expression E involving the variables of the guard condition of the loop, such that: 4 CS312 10/17/2006 CLASS NOTES 1) The value of E decreases by at least 1 each time around the loop 2) The loop condition is false if E is as low as some specified constant. Here when i = n+1 we don’t get into the loop but if i = n+1 is the index after ith iteration then (n+1-1) smallest no of elements of array A [1…. n] will be sorted in the subarray of A [1…. (n+1-1)]. Insertion sort Insertion sort works by taking elements from the list one by one and inserting them in their correct position into a new-sorted list. In arrays, the new list and the remaining elements can share the array's space, but insertion is expensive, requiring shifting all following elements over by one. The insertion sort works just like its name suggests - it inserts each item into its proper place in the final list. The simplest implementation of this requires two list structures - the source list and the list into which sorted items are inserted. To save memory, most implementations use an in-place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place. Proof same kind of loop Invariant. ----------------TO BE CONTINUED NEXT CLASS---------------- 5