1 / 27

Chapter 7 Sorting

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 a record with the specified key is to examine the list in left-to-right or right-to-left order. Sequential search. Sequential Search.

cyndi
Télécharger la présentation

Chapter 7 Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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; } • To search the key ‘22’: 0 1 2 3 4 5 23 2 7 15 42 12 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 intBinarySearch(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; } middle To find 23, left middle right 0 1 2 3 4 5 23 42 12 7 2 15 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 1 2 3 4 5 6 7 8 9 0 1 5 6 7 9 8 9 11 12 11 23 12 23 8

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

  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.

  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: pivot a: Find a pivot from a. a’: ≦ pivot ≧pivot pivot Quick Sort 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. pivot 4 1 5 1 2 6 4 j right left i 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 2 < pivot and should go to the other side. 4 1 1 5 2 4 4 1 5 2 6 4 pivot right j left i Interchange a[i] and a[j] 5 > pivot and should go to the other side. a[j] will eventually stop at a position where a[j] < pivot. Interchange a[j] and pivot. Stop.

  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;); } }

  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(n2); 1 2 4 5 6

  22. Lemma 7.1 • Let Tavg(n) be the expect time for function QuickSort() to sort a list with n records. Then there exists a constant k such that Tavg(n)≦knlogen 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-1 n-j j where c is a constant.

  24. Induction base:for n = 2 • Induction hypothesis: • Assume that • Induction step

More Related