Download presentation
Presentation is loading. Please wait.
Published byBeryl Price Modified over 9 years ago
1
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5
2
Recursion Recursive procedure/function is one that calls itself. A recursive procedure can be repeated with different parameter values. Characteristics: One or more simple cases of the problem (stopping/base cases) have a simple, non-recursive solution. The other cases of the problem can be reduced to problems that are closer to “stopping cases”. Eventually problem is reduced to stopping case/s. Stopping case/s is/are easy to solve.
3
Mergesort Recursive algorithm unroll recursion for direct implementation Algorithm: General idea: divide the problem in half, sorts each half separately and then merge the two sorted halves. Implementation: Mergesort ( A, first, last) if first < last then mid = [first + last]/2 mergesort(A, first, mid) mergesort(A, mid+1, last) merge ( A, first, mid, last) Procedure mergesort [A, first, last] sorts the elements in the sub array A[first…last]. If first last then sub-array has at most 1 element and is, therefore, sorted. Otherwise the divide step, finds mid & divides the array into two nearly equal parts
4
Mergesort 26 5 77 1 61 11 59 15 48 19 5 261 7711 6115 59 577264815591161119 19 48 1 5 26 77 11 15 59 61 19 48 1 5 11 15 26 59 61 77 19 48 1 5 11 15 19 26 48 59 61 77 Start pair-wise from array Merge pairs into 4-tuples, 4-tuples into 8-tuples & so on Time complexity: O(n log n) Bottom-up non-recursive version
5
Mergesort: Top down approach 26 5 77 1 61 11 59 15 48 19 26 5 77 1 6111 59 15 48 19 26 5 77 1 6115 48 1911 59 265771 6111591548 19 265771115915486119
6
Mergesort: Top down approach 5 261 7711 6115 59 577264815591161119 19 48 1 5 26 77 11 15 59 61 19 48 1 5 11 15 26 59 61 77 19 48 1 5 11 15 19 26 48 59 61 77
7
Divide and Conquer paradigm Divides the problem into several smaller problems of the same type. Combines the smaller problem’s solutions to solution of original problem. Provide direct solution for very small cases Time requirement: - time for division - time for combination - time for direct solution Ex: Mergesort, Quicksort, Max and Min
8
Bucket Sort- linear time complexity Algorithm General Idea:For reasonable sized integers e.g. 1, 2, …., m, use the value of each integer as the name of a bucket and so m buckets. Distribute the n input numbers into the ‘m’ buckets. To produce the output, empty the buckets in order, hauling each bucket as a queue (FIFO). Implementation: Bucketsort ( A) n = length [A] for i = 1 to n insert A[i ] into B[i] for i = 0 to n -1 sort list b[i] with insertion sort concatenate list B[0], B[1], ….. …. B[n-1] together 1 4 A B Key 12345671234567 7 2 1 4 1 4 6 12345671234567 1 2 4 6 7
9
Radix Sort- linear time complexity Algorithm General Idea: Sorts a set of numbers by iterated bucket sorting on least significant digit first. Then second least and so on. E.g. sorting seven 3-digit numbers 329 457 657 839 436 720 355 720 355 436 457 657 329 839 329 355 436 457 657 720 839 720 329 436 839 355 457 657 Sorting on 0, 1st & 2nd decimal place Implementation: Radixsort( A, d) for i = 1 to d use a stable sort to sort array A on digit i
10
Comparison based sorting vs indexed buckets Comparison based sorting determine the sorted order on an input array by comparing elements. Using decision tree model we can prove that the lower bound on any comparison sort to sort ‘n’ input elements for worst is O(n log n). Sorting with bucket sort takes O(n) time
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.