H-SC COMS 262 - Lecture 35 - Quick Sort

Unformatted text preview:

Quick SortThe Quick SortThe Quick Sort AlgorithmSlide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Example: The Quick SortSlide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36The qsort() Library FunctionThe qsort() PrototypeThe Function compar()01/13/19Sorting 1Quick SortLecture 35Section 13.3Thu, Apr 17, 200801/13/19Sorting 2The Quick SortThe Quick Sort is, on average, the fastest known sorting algorithm.The run time is of order n log n (with probability near 100%).01/13/19Sorting 3The Quick Sort AlgorithmChoose an element of the list to be the pivot element and copy it to a temporary location.Choose the first element, or Choose the last element, or Choose the middle element, or Choose a random element, or Choose the median of a small number of elements.01/13/19Sorting 4The Quick Sort AlgorithmThe Quick Sort is at its fastest when the pivot is the element that “belongs” in the middle of the list.It evenly divides the list into halves.The Quick Sort is at its slowest when the pivot is the element that “belongs” at one end of the list.All the elements end up on the same side.01/13/19Sorting 5The Quick Sort AlgorithmThe list is divided into four regions.Smaller region: The elements determined to be less than the pivot.Equal region: The elements determined to be equal to the pivot.Larger region: The elements determined to be greater than the pivot.NotDone region: The elements that have not yet been compared to the pivot.01/13/19Sorting 6The Quick Sort AlgorithmThe regions are arranged from left to right in the order Smaller regionEqual regionNotDone regionLarger regionInitially, all elements are in the NotDone region.Eventually, all elements will be in the other three regions.01/13/19Sorting 7The Quick Sort AlgorithmEstablish three indexes.smallerR - Index of the rightmost member of the Smaller region.notDoneL - Index of the leftmost member of the NotDone region.largerL - Index of the leftmost member of the Larger region.01/13/19Sorting 8The Quick Sort AlgorithmInitial ValuessmallerR is initialized to 1 less than the lowest subscript in the array.notDoneL is initialized to the lowest subscript in the array. largerL is initialized to 1 greater than the highest subscript in the array.01/13/19Sorting 9The Quick Sort AlgorithmCompare the element in position notDoneL to the pivot element.If it is less than the pivot, Increment smallerR, Swap the elements in the notDoneL and smallerR positions. Increment notDoneL.01/13/19Sorting 10The Quick Sort AlgorithmIf it is equal to the pivot, increment notDoneL. If it is greater than the pivot, Decrement largerL. Swap the elements in the notDoneL and largerL positions. Continue until the NotDone region is empty.01/13/19Sorting 11The Quick Sort AlgorithmThen apply the process recursively to the regions Smaller and Larger.01/13/19Sorting 12Example: The Quick SortPivot is 40.60 70 80 20 40 50 10 30 90SRLLNDL01/13/19Sorting 13Example: The Quick SortPivot is 40.90 70 80 20 40 50 10 30 60SRLLNDL01/13/19Sorting 14Example: The Quick SortPivot is 40.30 70 80 20 40 50 10 90 60SRLLNDL01/13/19Sorting 15Example: The Quick SortPivot is 40.30 70 80 20 40 50 10 90 60SRLLNDL01/13/19Sorting 16Example: The Quick SortPivot is 40.30 10 80 20 40 50 70 90 60SRLLNDL01/13/19Sorting 17Example: The Quick SortPivot is 40.30 10 80 20 40 50 70 90 60LLNDLSR01/13/19Sorting 18Example: The Quick SortPivot is 40.30 10 50 20 40 80 70 90 60LLNDLSR01/13/19Sorting 19Example: The Quick SortPivot is 40.30 10 40 20 50 80 70 90 60LLNDLSR01/13/19Sorting 20Example: The Quick SortPivot is 40.30 10 40 20 50 80 70 90 60LLNDLSR01/13/19Sorting 21Example: The Quick SortPivot is 40.30 10 20 40 50 80 70 90 60LLNDLSR01/13/19Sorting 22Example: The Quick SortNow apply the algorithm recursively to the sublists {30, 10, 20} and {50, 80, 70, 90, 60}.30 10 20 40 50 80 70 90 60LLNDLSR01/13/19Sorting 23Example: The Quick SortSublist {30, 10, 20}.Pivot = 10.30 10 20 40 50 80 70 90 60LLNDLSR01/13/19Sorting 24Example: The Quick SortSublist {30, 10, 20}.Pivot = 10.20 10 30 40 50 80 70 90 60LLNDLSR01/13/19Sorting 25Example: The Quick SortSublist {30, 10, 20}.Pivot = 10.10 20 30 40 50 80 70 90 60LLNDLSR01/13/19Sorting 26Example: The Quick SortSublist {30, 10, 20}.Pivot = 10.10 20 30 40 50 80 70 90 60LLNDLSR01/13/19Sorting 27Example: The Quick SortAs a special case, if size = 2, just compare and swap if necessary.10 20 30 40 50 60 70 80 90LLNDLSR01/13/19Sorting 28Example: The Quick SortAs a special case, if size = 2, just compare.10 20 30 40 50 80 70 90 60LLNDLSR01/13/19Sorting 29Example: The Quick SortSublist {50, 80, 70, 90, 60}.Pivot = 70.10 20 30 40 50 80 70 90 60LLSRNDL01/13/19Sorting 30Example: The Quick SortSublist {50, 80, 70, 90, 60}.Pivot = 70.10 20 30 40 50 80 70 90 60LLSRNDL01/13/19Sorting 31Example: The Quick SortSublist {50, 80, 70, 90, 60}.Pivot = 70.10 20 30 40 50 60 70 90 80LLSRNDL01/13/19Sorting 32Example: The Quick SortSublist {50, 80, 70, 90, 60}.Pivot = 70.10 20 30 40 50 60 70 90 80LLSRNDL01/13/19Sorting 33Example: The Quick SortSublist {50, 80, 70, 90, 60}.Pivot = 70.10 20 30 40 50 60 70 90 80LLSRNDL01/13/19Sorting 34NDLExample: The Quick SortSublist {50, 80, 70, 90, 60}.Pivot = 70.10 20 30 40 50 60 70 90 80LLSR01/13/19Sorting 35Example: The Quick SortSublist {50, 60}.10 20 30 40 50 60 70 90 8001/13/19Sorting 36Example: The Quick SortSublist {90, 80}.10 20 30 40 50 60 70 90 8001/13/19Sorting 37The qsort() Library FunctionThe Standard C/C++ Library contains the function qsort() that performs a Quick Sort on an array.01/13/19Sorting 38The qsort() Prototypewhere base is the address of the first element of the array. nmemb is the number of members in the array. size is the number of bytes in a single array element. compar() is a function that compares two array elements. void qsort(void* base, size_t nmemb, size_t size, int (*compar) (const void*, const void*));01/13/19Sorting 39The Function compar()The function compar() returns a negative integer if the first element is less than the second element. zero if the first element is equal to the second element. a


View Full Document

H-SC COMS 262 - Lecture 35 - Quick Sort

Download Lecture 35 - Quick Sort
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 35 - Quick Sort and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 35 - Quick Sort 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?