308-203A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000 Problem Definition Sorting: Given an ordered list of elements (x1, x2, x3, x4, … xn) on which there is an ordering relation, rearrange them in ascending order. Example: (8, 2, 9, 16, 3) (2, 3, 8, 9, 16) Note We will just talk about sorting lists of integers for demonstrative purposes, but a more real-world application would be to sort a database of students by alphabetical order. Insertion Sort (a.k.a. Bubblesort) Divide the list into sorted and not-yet-sorted parts. SORTED UNSORTED 1, 3, 7, 10, 15, 8, 78, 34, 82, 51, 17, 91, 123, 64 Each iteration: grow the sorted part by inserting the first element from the unsorted part Insertion Sort - pseudocode INSERTION-SORT(A[]) ::= { For i := 2 to n key := A[i] j := i -1; while (j > 0) and (A[j] > key) do A[j+1] := A[j] j := j - 1 A[j+1] := key } Order of Growth? INSERTION-SORT(A[]) ::= { For i := 2 to n key := A[i] LOOP 1 Q(1) j := i -1; while (j >= 0) and (A[j] > key) do A[j+1] := A[j] Q(1) j := j - 1 LOOP 2 Q(1) A[j+1] := key } Order of Growth? Order of Growth: Total Time = Q ( n ) + O ( n2 + n/2) = O ( n2 ) Proof on separate handout Any questions? Can We Do Better? Definition: Divide-and-Conquer means breaking a problem into smaller parts which can be solved separately and then combining the solutions to solve the original problem We can apply a Divide-and-Conquer strategy to the sorting problem…. Merge sort 1) Break the list in two 2) Assume we can sort the smaller lists (recursion) 3) Merge the results together Merge Sort Pseudocode MERGE-SORT(A[ ]) { if (n > 1) MERGE-SORT(A[1 … n/2 ]) MERGE-SORT(A[ n/2 + 1 … n]) MERGE(A[1 … n]) } Example 8, 78, 34, 82, 51, 17, 2, 91, 123, 64 1. Split 8, 78, 34, 82, 51 17, 2, 91, 123, 64 2. Recursion 8, 34, 51, 78, 82 3. Merge 2, 17, 64, 91, 123 2, 8, 17, 34, 51, 64, 78, 82, 91, 123 Order of Growth: A Recurrence Equation Note: Merging can be done in Q ( n ) [ can you see how??? ] T(n) = Q ( n ) + 2 T( n 2 ) So what does this really mean… Solving the Recurrence You can prove by induction that this means: T(n) = O( n log n ) Since O( n log n ) = o ( n2 ), we’ve made headway over Insertion-Sort Can We Still Do Better? No!! O( n log n ) is optimal for the sorting problem. This can be proven by showing that W ( n log n ) comparisons may be needed to establish the correct order. Any questions?