IT 60101: Lecture #181 Foundation of Computing Systems Lecture 18 Sorting Algorithms III
IT 60101: Lecture #182 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 #183 Simple Merging
IT 60101: Lecture #184 Simple Merging
IT 60101: Lecture #185 Simple Merging
IT 60101: Lecture #186 Simple Merging: Complexity Analysis
IT 60101: Lecture #187 Simple Merging: Complexity Analysis
IT 60101: Lecture #188 Simple Merging: Complexity Analysis
IT 60101: Lecture #189 Simple Merging: Complexity Analysis
IT 60101: Lecture #1810 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 #1811 (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 #1812 Merge Sort: Illustration
IT 60101: Lecture #1813 Merge Sort: Illustration
IT 60101: Lecture #1814 Merge Sort: Complexity Analysis Time complexity if n > 1 if n = 1
IT 60101: Lecture #1815 Merge Sort: Complexity Analysis For simplicity of calculation if n = 1 if n > 1 Assuming n = 2 k
IT 60101: Lecture #1816 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 #1817 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 #1818 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 #1819 Radix Sort Radix in number systems Number systemRadixBaseExample Binary0 and , 1101, 1111 Decimal0,1,2,3,4,5,6,7,8,910326,12345,1508 Alphabetica, b,….,z A, B,….,Z 26Ananya, McGraw AlphanumericA,…z, A….Z and 0…93606IT CS5201
IT 60101: Lecture #1820 Radix Sort: Example
IT 60101: Lecture #1821 Radix Sort: Example
IT 60101: Lecture #1822 Radix Sort: Example
IT 60101: Lecture #1823 Radix Sort: Example
IT 60101: Lecture #1824 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 #1825 Complexity Analysis: Summary MemoryTimeRemark Here, b denotes the base of the number system and a, c, d, and e are constants Complexity for ComplexityRemark Memory Irrespective of arrangements of elements. Time
IT 60101: Lecture #1826 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 #1827 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 #1828 Bucket Sort: Illustration
IT 60101: Lecture #1829 Bucket Sort: Illustration
IT 60101: Lecture #1830 Bucket Sort: Complexity Analysis Space complexity S(n) = n + 10 Time complexity where k > 0 is a constant
IT 60101: Lecture #1831 Complexity Analysis: Summary MemoryTimeRemark Here, c and k are some positive constants For large values of Complexity for ComplexityRemark Memory Time can be ignored
IT 60101: Lecture #1832 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 #1833 Counting Sort
IT 60101: Lecture #1834 Counting Sort: Complexity Analysis Self study
IT 60101: Lecture #1835 Summary of Distribution Sort
IT 60101: Lecture #1836 Summary of Distribution Sort
IT 60101: Lecture #1837 Summary of Distribution Sort
IT 60101: Lecture #1838 Summary of Distribution Sort