Presentation is loading. Please wait.

Presentation is loading. Please wait.

09.09.09IT 60101: Lecture #181 Foundation of Computing Systems Lecture 18 Sorting Algorithms III.

Similar presentations


Presentation on theme: "09.09.09IT 60101: Lecture #181 Foundation of Computing Systems Lecture 18 Sorting Algorithms III."— Presentation transcript:

1 09.09.09IT 60101: Lecture #181 Foundation of Computing Systems Lecture 18 Sorting Algorithms III

2 09.09.09IT 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

3 09.09.09IT 60101: Lecture #183 Simple Merging

4 09.09.09IT 60101: Lecture #184 Simple Merging

5 09.09.09IT 60101: Lecture #185 Simple Merging

6 09.09.09IT 60101: Lecture #186 Simple Merging: Complexity Analysis

7 09.09.09IT 60101: Lecture #187 Simple Merging: Complexity Analysis

8 09.09.09IT 60101: Lecture #188 Simple Merging: Complexity Analysis

9 09.09.09IT 60101: Lecture #189 Simple Merging: Complexity Analysis

10 09.09.09IT 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

11 09.09.09IT 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.

12 09.09.09IT 60101: Lecture #1812 Merge Sort: Illustration

13 09.09.09IT 60101: Lecture #1813 Merge Sort: Illustration

14 09.09.09IT 60101: Lecture #1814 Merge Sort: Complexity Analysis Time complexity if n > 1 if n = 1

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

16 09.09.09IT 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

17 09.09.09IT 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.

18 09.09.09IT 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

19 09.09.09IT 60101: Lecture #1819 Radix Sort Radix in number systems Number systemRadixBaseExample Binary0 and 120100, 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…93606IT6014 05CS5201

20 09.09.09IT 60101: Lecture #1820 Radix Sort: Example

21 09.09.09IT 60101: Lecture #1821 Radix Sort: Example

22 09.09.09IT 60101: Lecture #1822 Radix Sort: Example

23 09.09.09IT 60101: Lecture #1823 Radix Sort: Example

24 09.09.09IT 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

25 09.09.09IT 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

26 09.09.09IT 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

27 09.09.09IT 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.

28 09.09.09IT 60101: Lecture #1828 Bucket Sort: Illustration

29 09.09.09IT 60101: Lecture #1829 Bucket Sort: Illustration

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

31 09.09.09IT 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

32 09.09.09IT 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.

33 09.09.09IT 60101: Lecture #1833 Counting Sort

34 09.09.09IT 60101: Lecture #1834 Counting Sort: Complexity Analysis Self study

35 09.09.09IT 60101: Lecture #1835 Summary of Distribution Sort

36 09.09.09IT 60101: Lecture #1836 Summary of Distribution Sort

37 09.09.09IT 60101: Lecture #1837 Summary of Distribution Sort

38 09.09.09IT 60101: Lecture #1838 Summary of Distribution Sort


Download ppt "09.09.09IT 60101: Lecture #181 Foundation of Computing Systems Lecture 18 Sorting Algorithms III."

Similar presentations


Ads by Google