Chapter 8, Sorting
Sorting, Ordering, or Sequencing “Since only two of our tape drives were in working order, I was ordered to order more tape units in short order, in order to order the data several orders of magnitude faster.” “I was sort of out of sorts after sorting that sort of data.” D. E. Knuth, The Art of Computer Programming, Volume 3, 2 nd Edition, p.1 & 2.
Sorting and Computational Complexity Straight insertion - O(N 2 ) Shell’s method – O(N 3/2 ) Quick sort and heap sort – O(N log N) N CPU time (sec)
Straight Insertion Initial order is already in good order Swap 1 with earlier entries repeatedly Swap 2 repeatedly until something less than 2 … and so on
Straight Insertion Program
Quick Sort Divide the array in two groups, such that all members of the first group is smaller than the second group. Sort each group Combine the results
Quick Sort Pseudo-Code Quicksort(A[ ], p, r) { if (p < r) { then q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); } // sorting A[j] for p ≤ j ≤ r // split the data in two groups
Pseudo-Code Continued Partition(A[ ], p, r) { x = A[p]; i = p –1; j = r + 1; while(1) { do { --j; } while(A[j] > x); do { ++i; } while (A[i] < x); if(i<j) { swap(i,j); } else { return j; }
The Working of Code (During Partition) i j ij ij j i j i first group is given by numbers < x=5 [ ] [6 5 7] first group second group
Quick Sort in C void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *)); Function qsort( ) takes an array base[ ] of n elements, each of which size bytes. The member of the array is sorted according to ascending order based on the comparison defined by the function cmp().
A Heap Element is such that a j/2 ≥ a j ≤ ≥ ≤ ≥ ≤ ≥
How to Make a Heap Build the heap recursively from bottom up. If two child branches are already valid heaps but the current parent node is not a larger number, swap with the larger of the children (then child’s child, and so on). Sorting from small to large by removing the top of the heap, replacing it with the last unsorted, one by one.
How hpsort() Works?hpsort() The animation at the following web page is helpful to understand heap sort operations: /applets/sortingII/heapSort/heap.html /applets/sortingII/heapSort/heap.html
Problem set 5 1. Consider the input data array {3,7,2,1,8,5,4,6} of size 8. Work out the steps for three types of sorting algorithms: insert sort, quick sort, and heap sort. 2. Argue or demonstrate qualitatively that heap sort takes O(N log N) cputime.