1 / 38

Foundation of Computing Systems

Foundation of Computing Systems. Lecture 18 Sorting Algorithms III. Sorting by Merging. A class of sorting algorithms based on the principle of “merging” or “collating” The principle is easily amenable to divide and conquer technique.

Download Presentation

Foundation of Computing Systems

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. Foundation of Computing Systems Lecture 18 Sorting Algorithms III IT 60101: Lecture #18

  2. Sorting by Merging • A class of sorting algorithms based on the principle of “merging” or “collating” • The principle is easily amenable to divide and conquer technique. • Suitable for sorting very large list, even the entire list is not necessarily to be residing in main memory. • Merging is the main policy IT 60101: Lecture #18

  3. Simple Merging IT 60101: Lecture #18

  4. Simple Merging IT 60101: Lecture #18

  5. Simple Merging IT 60101: Lecture #18

  6. Simple Merging: Complexity Analysis IT 60101: Lecture #18

  7. Simple Merging: Complexity Analysis IT 60101: Lecture #18

  8. Simple Merging: Complexity Analysis IT 60101: Lecture #18

  9. Simple Merging: Complexity Analysis IT 60101: Lecture #18

  10. Complexity Analysis: Summary • Best case: • T(n) = min(n1, n2) = 1 if n1 = 1 or n2 = 1 • Worst case: • T(n) = n-1 • Average case: • T(n) = n/2 IT 60101: Lecture #18

  11. (Internal) Merge Sort • Let a list of n elements to be sorted with l and r being the position of leftmost and rightmost elements in the list. • Divide: • Partition the list midway, that is, at into two sub lists with elements in each, if n is even or and elements if n is odd. • Conquer: • Sort the two lists recursively using the merge sort. • Combine: • Merge the sorted sub lists to obtain the sorted output. IT 60101: Lecture #18

  12. Merge Sort: Illustration IT 60101: Lecture #18

  13. Merge Sort: Illustration IT 60101: Lecture #18

  14. Merge Sort: Complexity Analysis • Time complexity if n > 1 if n = 1 IT 60101: Lecture #18

  15. Merge Sort: Complexity Analysis • For simplicity of calculation if n = 1 if n > 1 Assuming n = 2k IT 60101: Lecture #18

  16. Quick Sort vs. Merge Sort • Quick sort • hard division, easy combination • partition in the divide step of the divide-and-conquer framework • hence combine step does nothing • Merge sort • easy division, hard combination • merge in the combine step • the divide step in this framework does one simple calculation only IT 60101: Lecture #18

  17. Quick Sort vs. Merge Sort • Both the algorithms divide the problem into two sub problems • Merge sort: • two sub problems are of almost equal size always. • Quick sort: • an equal sub division is not guaranteed. • This difference between the two sorting methods appears as the deciding factor of their run time performances. IT 60101: Lecture #18

  18. Sorting by Distribution • Principle • Sorting without key comparison • Running time complexity is O(n) • Sorting Techniques • Radix sort • Bucket sort • Counting sort IT 60101: Lecture #18

  19. Radix Sort • Radix in number systems IT 60101: Lecture #18

  20. Radix Sort: Example IT 60101: Lecture #18

  21. Radix Sort: Example IT 60101: Lecture #18

  22. Radix Sort: Example IT 60101: Lecture #18

  23. Radix Sort: Example IT 60101: Lecture #18

  24. Radix Sort: Complexity Analysis Let, a = time to extract a component from an element. e = time to enqueue an element in an array. d = time to dequeue an element from an array. • Time for distribution operation = (a+e)n • Time for combination = (d+e)n • Time complexity IT 60101: Lecture #18

  25. Complexity Analysis: Summary IT 60101: Lecture #18

  26. Bucket Sort • Radix sort is efficient for a list of small size and when the size of keys is not too large. • Radix sort demands huge memory as auxiliary memory. • For example, if the radix is a digit sort then it requires 10 times extra storage space than the size of the list. • We can not afford lexicographic sort in a memory constrained application in spite of its O(n) running time complexity. • An solution to this is called Bucket sort IT 60101: Lecture #18

  27. Bucket Sort • Strategy • Distribution of n elements among 10 buckets (in general 10 << n). • Sort each bucket individually. • Combine all buckets to get the output list. IT 60101: Lecture #18

  28. Bucket Sort: Illustration IT 60101: Lecture #18

  29. Bucket Sort: Illustration IT 60101: Lecture #18

  30. Bucket Sort: Complexity Analysis • Space complexity S(n) = n + 10 • Time complexity where k > 0 is a constant IT 60101: Lecture #18

  31. Complexity Analysis: Summary For large values of can be ignored IT 60101: Lecture #18

  32. Counting Sort: Illustration • Principle • The basic idea of counting sort is to “count” for each element, say x, in the input list, the number of elements less than or equal to x. • With these counts place the elements directly in the output list. IT 60101: Lecture #18

  33. Counting Sort IT 60101: Lecture #18

  34. Counting Sort: Complexity Analysis • Self study IT 60101: Lecture #18

  35. Summary of Distribution Sort IT 60101: Lecture #18

  36. Summary of Distribution Sort IT 60101: Lecture #18

  37. Summary of Distribution Sort IT 60101: Lecture #18

  38. Summary of Distribution Sort IT 60101: Lecture #18

More Related