420 likes | 432 Vues
Learn the strategy, implementation, and correctness of Merge Sort and Quick Sort algorithms for sorting sequences effectively in computer science.
E N D
CMSC 202 Computer Science II for Majors
Topics • Sorting Techniques • Merge Sort • Quick Sort
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.
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.
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
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
Example We are given array of n integers to sort 40 20 10 80 60 50 7 30 100
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Partition Result 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
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]
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 }
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++ ;
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; } }