380 likes | 534 Views
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.
E N D
Foundation of Computing Systems Lecture 18 Sorting Algorithms III IT 60101: Lecture #18
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
Simple Merging IT 60101: Lecture #18
Simple Merging IT 60101: Lecture #18
Simple Merging IT 60101: Lecture #18
Simple Merging: Complexity Analysis IT 60101: Lecture #18
Simple Merging: Complexity Analysis IT 60101: Lecture #18
Simple Merging: Complexity Analysis IT 60101: Lecture #18
Simple Merging: Complexity Analysis IT 60101: Lecture #18
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
(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
Merge Sort: Illustration IT 60101: Lecture #18
Merge Sort: Illustration IT 60101: Lecture #18
Merge Sort: Complexity Analysis • Time complexity if n > 1 if n = 1 IT 60101: Lecture #18
Merge Sort: Complexity Analysis • For simplicity of calculation if n = 1 if n > 1 Assuming n = 2k IT 60101: Lecture #18
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
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
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
Radix Sort • Radix in number systems IT 60101: Lecture #18
Radix Sort: Example IT 60101: Lecture #18
Radix Sort: Example IT 60101: Lecture #18
Radix Sort: Example IT 60101: Lecture #18
Radix Sort: Example IT 60101: Lecture #18
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
Complexity Analysis: Summary IT 60101: Lecture #18
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
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
Bucket Sort: Illustration IT 60101: Lecture #18
Bucket Sort: Illustration IT 60101: Lecture #18
Bucket Sort: Complexity Analysis • Space complexity S(n) = n + 10 • Time complexity where k > 0 is a constant IT 60101: Lecture #18
Complexity Analysis: Summary For large values of can be ignored IT 60101: Lecture #18
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
Counting Sort IT 60101: Lecture #18
Counting Sort: Complexity Analysis • Self study IT 60101: Lecture #18
Summary of Distribution Sort IT 60101: Lecture #18
Summary of Distribution Sort IT 60101: Lecture #18
Summary of Distribution Sort IT 60101: Lecture #18
Summary of Distribution Sort IT 60101: Lecture #18