Chapter 7 Sorting
Total Page:16
File Type:pdf, Size:1020Kb
CHAPTER 7 SORTING All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures in C”, Computer Science Press, 1992. 1 Sequential Search Example 44, 55, 12, 42, 94, 18, 06, 67 unsuccessful search – n+1 successful search n n+ 1 ∑ (i)/n= i= 1 2 2 Program:Sequential search int seqsearch( int list[ ], int searchnum, int n ) { /*search an array, list, that has n numbers. Return -1, if search num is not in the list */ int i; for (i=0; i<n && list[i] != searchnum ; i++) ; return (( i<n) ? i : -1); } 3 Program: Binary search int binsearch(element list[ ], int searchnum, int n) { /* search list [0], ..., list[n-1]*/ int left = 0, right = n-1, middle; while (left <= right) { middle = (left+ right)/2; switch (COMPARE(list[middle].key, searchnum)) { case -1: left = middle +1; break; case 0: return middle; case 1:right = middle - 1; } } O(log2n) return -1; } 4 Sorting Problem Definition – given (R0, R1, …, Rn-1), where Ri = key + data find a permutation , such that K(i-1) K(i), 0<i<n-1 sorted – K(i-1) K(i), 0<i<n-1 stable – if i < j and Ki = Kj then Ri precedes Rj in the sorted list internal sort vs. external sort criteria – # of key comparisons – # of data movements 5 Insertion Sort Insertion sort is a simple sorting algorithm that is appropriate for small inputs. Most common sorting technique used by card players. The list is divided into two parts: sorted and unsorted. In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place. A list of n elements will take at most n-1 passes to sort the data. 6 Sorted Unsorted 23 78 45 8 32 56 Original List 23 78 45 8 32 56 After pass 1 23 45 78 8 32 56 After pass 2 After pass 3 8 23 45 78 32 56 After pass 4 8 23 32 45 78 56 After pass 5 8 23 32 45 56 78 7 Insertion Sort Find an element smaller than K. 26 5 77 1 61 11 59 15 48 19 5 26 77 1 61 11 59 15 48 19 5 26 77 1 61 11 59 15 48 19 1 5 26 77 61 11 59 15 48 19 1 5 26 61 77 11 59 15 48 19 1 5 11 26 61 77 59 15 48 19 1 5 11 26 59 61 77 15 48 19 1 5 11 15 26 59 61 77 48 19 1 5 11 15 26 48 59 61 77 19 5 26 1 61 11 59 15 48 19 77 8 Insertion Sort void insertion_sort(element list[], int n) { int i, j; element next; for (i=1; i<n; i++) { next= list[i]; for (j=i-1; j>=0&&next.key<list[j].key; j--) list[j+1] = list[j]; list[j+1] = next; } insertion_sort(list,n) } 9 worse case i 0 1 2 3 4 - 5 4 3 2 1 n−2 1 4 5 3 2 1 O ( ∑ i )=O (n 2) 2 3 4 5 2 1 j= 0 3 2 3 4 5 1 4 1 2 3 4 5 best case i 0 1 2 3 4 - 2 3 4 5 1 1 2 3 4 5 1 O(n) 2 2 3 4 5 1 3 2 3 4 5 1 4 1 2 3 4 5 10 44 55 12 42 94 18 06 67 11 Variation Binary insertion sort – sequential search --> binary search – reduce # of comparisons, # of moves unchanged List insertion sort – array --> linked list – sequential search, move --> 0 12 Selection Sort The list is divided into two sublists, sorted and unsorted, which are divided by an imaginary wall. We find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. A list of n elements requires n-1 passes to completely rearrange the data. 13 Sorted Unsorted 23 78 45 8 32 56 Original List 8 78 45 23 32 56 After pass 1 8 23 45 78 32 56 After pass 2 After pass 3 8 23 32 78 45 56 After pass 4 8 23 32 45 78 56 After pass 5 8 23 32 45 56 78 14 Selection Sort Cont.... void selectionSort( Item a[], int n) { for (int i = 0; i < n-1; i++) { int min = i; for (int j = i+1; j < n; j++) if (a[j] < a[min]) min = j; swap(a[i], a[min]); } } 15 Selection Sort Analysis The inner for loop executes the size of the unsorted part minus 1 (from 1 to n-1), and in each iteration we make one key comparison. # of key comparisons = 1+2+...+n-1 = n*(n-1)/2 So, Selection sort is O(n2) The best case, the worst case, and the average case of the selection sort algorithm are same. all of them are O(n2) This means that the behavior of the selection sort algorithm does not depend on the initial organization of data. Since O(n2) grows so rapidly, the selection sort algorithm is appropriate only for small n. Although the selection sort algorithm requires O(n2) key comparisons, it only requires O(n) moves. A selection sort could be a good choice if data moves are costly but key comparisons are not costly (short keys, long records). 16 Bubble Sort The list is divided into two sublists: sorted and unsorted. The smallest element is bubbled from the unsorted list and moved to the sorted sublist. After that, the wall moves one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. Each time an element moves from the unsorted part to the sorted part one sort pass is completed. Given a list of n elements, bubble sort requires up to n-1 passes to sort the data. 17 Bubble Sort 23 78 45 8 32 56 Original List 8 23 78 45 32 56 After pass 1 8 23 32 78 45 56 After pass 2 After pass 3 8 23 32 45 78 56 After pass 4 8 23 32 45 56 78 18 Bubble Sort Cont.... void bubleSort(Item a[], int n) { bool sorted = false; int last = n-1; for (int i = 0; (i < last) && !sorted; i++){ sorted = true; for (int j=last; j > i; j--) if (a[j-1] > a[j]{ swap(a[j],a[j-1]); sorted = false; // signal exchange } } 19 Bubble Sort Analysis Best-case: O(n) Array is already sorted in ascending order. The number of moves: 0 O(1) The number of key comparisons: (n-1) O(n) Worst-case: O(n2) Array is in reverse order: Outer loop is executed n-1 times, The number of moves: 3*(1+2+...+n-1) = 3 * n*(n-1)/2 O(n2) The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 O(n2) Average-case: O(n2) We have to look at all possible initial data organizations. So, Bubble Sort is O(n 2) 20 Quick Sort (C.A.R. Hoare) Given (R0, R1, …, Rn-1) Ki: pivot key if Ki is placed in S(i), then Kj Ks(i) for j < S(i), Kj Ks(i) for j > S(i). R0, …, RS(i)-1, RS(i), RS(i)+1, …, RS(n-1) two partitions 21 Example for Quick Sort R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 left right { 26 5 37 1 61 11 59 15 48 19} 0 9 { 11 5 19 1 15} 26 { 59 61 48 37} 0 4 { 1 5} 11 {19 15} 26 { 59 61 48 37} 0 1 1 5 11 15 19 26 { 59 61 48 37} 3 4 1 5 11 15 19 26 { 48 37} 59 { 61} 6 9 1 5 11 15 19 26 37 48 59 { 61} 6 7 1 5 11 15 19 26 37 48 59 61 9 9 1 5 11 15 19 26 37 48 59 61 22 Quick Sort void quicksort(element list[], int left, int right) { int pivot, i, j; element temp; if (left < right) { i = left; j = right+1; pivot = list[left].key; do { do i++; while (list[i].key < pivot); do j--; while (list[j].key > pivot); if (i < j) SWAP(list[i], list[j], temp); } while (i < j); SWAP(list[left], list[j], temp); quicksort(list, left, j-1); quicksort(list, j+1, right); } } 23 Analysis for Quick Sort Assume that each time a record is positioned, t he list is divided into the rough same size of tw o parts. Position a list with n element needs O(n) T(n) is the time taken to sort n elements T(n)<=cn+2T(n/2) for some c <=cn+2(cn/2+2T(n/4)) ... <=cnlog n+nT(1)=O(nlog n) 24 Time and Space for Quick Sort Space complexity: – Average case and best case: O(log n) – Worst case: O(n) Time complexity: – Average case and best case: O(n log n) 2 – Worst case: O(n ) 25 Merge Sort Given two sorted lists (list[i], …, list[m]) (list[m+1], …, list[n]) generate a single sorted list (sorted[i], …, sorted[n]) O(n) space vs.