1 / 42

Efficient Merge Sort and Quick Sort Algorithms

Learn the strategy, implementation, and correctness of Merge Sort and Quick Sort algorithms for sorting sequences effectively in computer science.

erodriquez
Download Presentation

Efficient Merge Sort and Quick Sort Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Computer Science II for Majors

  2. Topics • Sorting Techniques • Merge Sort • Quick Sort

  3. Merge Sort • Problem Statement: Sort a sequence in an efficient manner. • Strategy: • Divide input into smaller sequences. • Sort smaller sequences. • Merge resulting sorted sequences together.

  4. Merge-Sort Algorithm • Recursive, divide-and-conquer strategy. • Base case: A sequence with exactly one element in it. • To sort a sequence of n>1 elements: • Divide the sequence into two sequences of length floor(n/2) and ceiling(n/2). • Recursively sort each of the two subsequences; and then, • Merge the sorted subsequences to obtain the final result.

  5. Merge Sort Example

  6. Merging Two Sequences

  7. Merge Sort

  8. Merge Sort

  9. Correctness of Merge Sort • Correct on one element -- returns with no further sorting • More than one element -- sort each half (closer to base case) • Assuming Merge works correctly, merged array will be sorted • We are really passing off all the work of sorting into Merge

  10. Quicksort • Another divide-and-conquer algorithm • The array A[p..r] is partitioned into two non-empty subarrays A[p..q] and A[q+1..r] • Invariant: All elements in A[p..q] are less than all elements in A[q+1..r] • The subarrays are recursively sorted by calls to Quicksort • Unlike merge sort, no combining step: two subarrays form an already-sorted array

  11. Example We are given array of n integers to sort 40 20 10 80 60 50 7 30 100

  12. Select Pivot Element There are a number of ways to pick the pivot element. In this example, we will use the first element in the array i.e. 40. 40 20 10 80 60 50 7 30 100

  13. Quick Sort Example 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  14. Quick Sort Example 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  15. Quick Sort Example 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  16. Quick Sort Example 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  17. Quick Sort Example 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  18. Quick Sort Example 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  19. Quick Sort Example 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  20. Quick Sort Example 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  21. Quick Sort Example 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  22. Quick Sort Example 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  23. Quick Sort Example 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  24. Quick Sort Example 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  25. Quick Sort Example 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  26. Quick Sort Example 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  27. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  28. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  29. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  30. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  31. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  32. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  33. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  34. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  35. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  36. Quick Sort Example 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  37. Quick Sort Example 7 20 10 30 40 50 60 80 100 pivot_index = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index

  38. Partition Result 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]

  39. Recursion: Quicksort Sub-arrays 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]

  40. QuickSort Algorithm void QuickSort( int array[], int start, int end) { if ( start == end ) return; // Only one element int middle = Partition( array, start, end); // Reposition elements QuickSort( array, start, middle); // Sort left half QuickSort( array, middle + 1, end); // Sort right half }

  41. Quick Sort int Partition( int array[], int start, int end) { int pivot = array[start]; // Smaller than pivot on left; larger on right int left = start; // First element int right = end; // Last element while ( 1 ) // Loop forever; return once partitioning is completed { // starting on right, find an element <= pivot while ( array[right] > pivot && right >= start ) right-- ; // starting on left, find an element >= pivot while ( array[left] < pivot && left <= end ) left++ ;

  42. Quick Sort if ( left < right ) { //swap elements if halves aren't complete int temp = array[left]; array[left] = array[right]; array[right] = temp; // Skip over exchanged values left++ ; right-- ; } else // Otherwise, return location of pivot return right; } }

More Related