Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for.

Similar presentations


Presentation on theme: "Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for."— Presentation transcript:

1 Chapter 7 Sorting Part I

2 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for a record with the specified key is to examine the list in left- to-right or right-to-left order. ◦ Sequential search.

3 Sequential Search To search the key ‘22’: int SeqSearch(int a[], int n, key) { int i; for (i=0; i<n && a[i] != key; i++) ; if (i >= n) return -1; return i; } int SeqSearch(int a[], int n, key) { int i; for (i=0; i<n && a[i] != key; i++) ; if (i >= n) return -1; return i; } 23 0 2 2 1 7 7 2 15 3 42 4 12 5 The search makes n key comparisons when it is unsuccessful.

4 Analysis of Time Complexity Worst case: ◦ O(n) when the search is unsuccessful. ◦ Each element is examined exactly once. Average case: ◦ When the search is successful, the number of comparison depends on the position of the search key.

5 Binary Search int BinarySearch(int a[], int n, in key) { int left = 0, right = n-1; while (left <= right) { int middle = (left + right) / 2; if (key < a[middle]) right = middle - 1; else if (key > a[middle]) left = middle + 1; else return middle; } return -1; } int BinarySearch(int a[], int n, in key) { int left = 0, right = n-1; while (left <= right) { int middle = (left + right) / 2; if (key < a[middle]) right = middle - 1; else if (key > a[middle]) left = middle + 1; else return middle; } return -1; } 2 2 0 7 7 1 12 2 15 3 23 4 42 5 leftrightmiddleTo find 23, middle found.

6 Binary Search Even when the search is unsuccessful, the time complexity is still O(log n). ◦ Something is to be gained by maintaining the list in an order manner.

7 Sorting Methods Internal: ◦ Can be carried out in memory.  Insertion sort.  Quick sort.  Merge sort.  Heap sort.  Radix sort. External: ◦ The dataset is much more bigger so the data cannot be fully carried out in memory.

8 7.2 INSERTION SORT

9 Idea Consider how to insert a new integer into a sorted array so that all the elements in the array are sorted. 0 0 1 1 5 2 6 3 7 4 9 5 11 6 12 7 23 89 8 91112238

10 Insertion into a Sorted List Suppose a is an integer array with n elements. void Insert(int key, int a[], int i); ◦ Insert a new value, key, into the first i integers in a, where the first i integers should be sorted. void Insert(int key, int a[], int i) { int j; for (j = i-1; j >= 0 && key < a[j]; j--) a[j+1] = a[j]; a[j+1] = key; } void Insert(int key, int a[], int i) { int j; for (j = i-1; j >= 0 && key < a[j]; j--) a[j+1] = a[j]; a[j+1] = key; }

11 Insertion Sort At the i th iteration of this algorithm, the first i elements in the original array will be sorted. 12 0 1 1 6 2 0 3 7 4 23 5 11 6 9 7 5 8 i = 1 1 12 a: temp: 1 i = 2 6 void InsertionSort(int key, int a[], int i) { for (j = 1; j < n; j++) int temp = a[j] Insert(temp, a, j-1); } void InsertionSort(int key, int a[], int i) { for (j = 1; j < n; j++) int temp = a[j] Insert(temp, a, j-1); }

12 Analysis of InsertionSort() Worst case ◦ As each new record is inserted into the sorted part of the list, the entire sorted part is shifted right by one position. j01234 -54321 145321 234521 323451 412345

13 Analysis of InsertionSort() In worst case, InsertSort() makes O(i) comparison before a[i] is inserted. For i=1, 2, …, n-1. The time complexity of InsertionSort() is ◦ In fact, insertion sort is about the fastest sorting method for small n (n ≦ 30).

14 Variations Binary Insertion Sort: ◦ To reduce the number of comparison. ◦ Therefore, apply binary search in Insert(). Linked Insertion Sort ◦ The elements of the list are represented as a linked list.  The number of record move can become zero because only link fields require adjustment.

15 7.3 QUICK SORT

16 Introduction Quick sort has the best average behavior among the sorting methods. Concept: a: pivot Find a pivot from a. pivot a’: ≧ pivot ≦ pivot Quick Sort Quick sort is essentially a recursive approach.

17 Definition left and right are used to indicate the scope of the array. ◦ left should be less than right; if not, return directly. a[left] is initially chosen as pivot. 4151264 left right pivot i j i: examined as index to scan the array from left to right. j: examined as index to scan the array from right to left. Initially, i = left, j=right+1.

18 Example 4151264 left right pivot i j 5 > pivot and should go to the other side. 2 < pivot and should go to the other side. Interchange a[i] and a[j] 25 Stop. a[j] will eventually stop at a position where a[j] < pivot. Interchange a[j] and pivot. 144

19 Algorithm void QuickSort(int a[], int left, int right) { if (left < right) { pivot = a[left]; i = left; j = right+1; while (i < j) { for (i++; i<=j and a[i] < pivot; i++) ; for (j++; i>=j and a[i] >= pivot; i++) ; if (i < j) interchages a[i] amd [i]; } QuickSort(a, left, j-1); QuickSort(a, j+1 right;); } void QuickSort(int a[], int left, int right) { if (left < right) { pivot = a[left]; i = left; j = right+1; while (i < j) { for (i++; i<=j and a[i] < pivot; i++) ; for (j++; i>=j and a[i] >= pivot; i++) ; if (i < j) interchages a[i] amd [i]; } QuickSort(a, left, j-1); QuickSort(a, j+1 right;); }

20 Analysis of QuickSort() Best case ◦ If the pivot is correctly positioned (left size = right size), the time complexity is

21 Analysis of QuickSort() Worst case: ◦ Consider a list is stored.  The smallest one is always chosen as pivot.  n iterations are required to reach base case (left >= right).  Each iteration takes O(n) time.  The time complexity is O(n 2 ); 16425

22 Lemma 7.1 Let T avg (n) be the expect time for function QuickSort() to sort a list with n records. Then there exists a constant k such that T avg (n) ≦ knlog e n for n ≧ 2.

23 Lemma 7.1 Proof: ◦ Assume pivot is at j. ◦ The expected time to sort two sides is ◦ The average time is j j-1n-j where c is a constant.

24

25 Induction base: for n = 2 Induction hypothesis: ◦ Assume that Induction step

26

27


Download ppt "Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for."

Similar presentations


Ads by Google