View
214
Download
2
Category
Preview:
Citation preview
Searching and Sorting
Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort
Lecture 5
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.
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
Mergesort26 5 77 1 61 11 59 15 48 19
5 26 1 77 11 61 15 59
5
77 26 48 15 59 11 61 1 19
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
Mergesort: Top down approach
26 5 77 1 61 11 59 15 48 19
26 5 77 1 61 11 59 15 48 19
26 5 77 1 61 15 48 19 11 59
26 5 77 1 61 11 59 15 48 19
26 5 77 1 11 59 15 4861 19
Mergesort: Top down approach
5 26 1 77 11 61 15 59
5
77 26 48 15 59 11 61 1 19
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
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
Bucket Sort- linear time complexityAlgorithm
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 BKey
1
2
3
4
5
6
7
7
2
1
4
1
4
6
1
2
3
4
5
6
7
1
2
4
6
7
Radix Sort- linear time complexityAlgorithm
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
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
Recommended