Searching and Sorting

# Searching and Sorting

Télécharger la présentation

## Searching and Sorting

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

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: 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 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)

4. 26 5 77 1 61 11 59 15 48 19 19 48 5 26 1 77 11 61 15 59 11 15 59 61 19 48 1 5 26 77 1 5 11 15 26 59 61 77 19 48 1 5 11 15 19 26 48 59 61 77 Mergesort Bottom-up non-recursive version 26 5 77 1 61 11 59 15 48 19 • Start pair-wise from array • Merge pairs into 4-tuples, • 4-tuples into 8-tuples • & so on • Time complexity: O(n log n)

5. 26 5 77 1 61 11 59 15 48 19 26 5 77 1 61 11 59 15 48 19 77 1 61 11 59 15 48 19 26 5 26 5 77 1 61 11 59 15 48 19 26 5 77 1 61 11 59 15 48 19 Mergesort: Top down approach

6. 26 5 77 1 61 11 59 15 48 19 19 48 5 26 1 77 11 61 15 59 11 15 59 61 19 48 1 5 26 77 1 5 11 15 26 59 61 77 19 48 1 5 11 15 19 26 48 59 61 77 Mergesort: Top down approach

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. B Key A 1 2 3 4 5 6 7 1 2 3 4 5 6 7 7 2 1 4 1 4 6 1 4 1 2 4 6 7 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

9. 329 457 657 839 436 720 355 720 355 436 457 657 329 839 720 329 436 839 355 457 657 329 355 436 457 657 720 839 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 Implementation: Radixsort( A, d) for i = 1 to d use a stable sort to sort array A on digiti Sorting on 0, 1st & 2nd decimal place

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