Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 8, Sorting

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

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

4 Straight Insertion 4512376 Initial order 4512376 5 is already in good order 1452376 Swap 1 with earlier entries repeatedly 1245376 Swap 2 repeatedly until something less than 2 … and so on

5 Straight Insertion Program

6 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

7 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

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

9 The Working of Code (During Partition) 5 3 2 6 4 1 3 7 3 3 2 6 4 1 5 7 3 3 2 1 4 6 5 7 i j ij ij j i j i first group is given by numbers < x=5 [ 3 3 2 1 4] [6 5 7] first group second group

10 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().

11 A Heap Element is such that a j/2 ≥ a j ≤ ≥ ≤ ≥ ≤ ≥

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

13 How hpsort() Works?hpsort() The animation at the following web page is helpful to understand heap sort operations: http://www.cse.iitk.ac.in/users/dsrkg/cs210 /applets/sortingII/heapSort/heap.html http://www.cse.iitk.ac.in/users/dsrkg/cs210 /applets/sortingII/heapSort/heap.html

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


Download ppt "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."

Similar presentations


Ads by Google