Presentation is loading. Please wait.

Presentation is loading. Please wait.

308-203A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.

Similar presentations


Presentation on theme: "308-203A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000."— Presentation transcript:

1 308-203A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000

2 Problem Definition Sorting: Given an ordered list of elements (x1, x2, x3, x4, … x n ) on which there is an ordering relation, rearrange them in ascending order. Example: (8, 2, 9, 16, 3) (2, 3, 8, 9, 16)

3 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.

4 Insertion Sort (a.k.a. Bubblesort) Divide the list into sorted and not-yet-sorted parts. 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 SORTED UNSORTED

5 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 }

6 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? LOOP 1  (1) LOOP 2

7 Order of Growth? Order of Growth: Total Time =  ( n ) + O ( n 2 + n/2) = O ( n 2 ) Proof on separate handout

8 Any questions?

9 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….

10 Merge sort 1) Break the list in two 2) Assume we can sort the smaller lists (recursion) 3) Merge the results together

11 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]) }

12 Example 8, 78, 34, 82, 51, 17, 2, 91, 123, 64 8, 78, 34, 82, 5117, 2, 91, 123, 64 8, 34, 51, 78, 82 2, 17, 64, 91, 123 2. Recursion 1. Split 3. Merge 2, 8, 17, 34, 51, 64, 78, 82, 91, 123

13 Order of Growth: A Recurrence Equation Note: Merging can be done in  ( n ) [ can you see how??? ] T(n) =  ( n ) + 2 T( ) n2n2 So what does this really mean…

14 Solving the Recurrence You can prove by induction that this means: T(n) = O( n log n ) Since O( n log n ) = o ( n 2 ), we’ve made headway over Insertion-Sort

15 Can We Still Do Better? No!! O( n log n ) is optimal for the sorting problem. This can be proven by showing that  ( n log n ) comparisons may be needed to establish the correct order.

16 Any questions?


Download ppt "308-203A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000."

Similar presentations


Ads by Google