Data Structure: Quick Sort

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

To partition

To partition the input: -the quicksort algorithm divides the array into two parts, referred to as the low partition and the high partition. - All values in the low partition are less than or equal to the pivot value. -All values in the high partition are greater than or equal to the pivot value. The values in each partition are not necessarily sorted. Ex: Partitioning {4 10 34 25 1} with a pivot value of 34 results in a low partition of {4 10 1} and a high partition of {34 25}. Values equal to the pivot may appear in either or both of the partitions.

pivot

can be any value within the array being sorted, commonly the value of the middle array element. Ex: For the list {4 10 34 25 1}, the middle element is located at index 2 (the middle of indices 0..4) and has a value of 34.

Quicksort

is a sorting algorithm that repeatedly partitions the input into low and high parts (each part unsorted), and then recursively sorts each of those parts. To partition the input, quicksort chooses a pivot to divide the data into low and high parts

Worst case quick sort

For typical unsorted data, such equal partitioning occurs. However, partitioning may yield unequal sized part in some cases. - If the pivot selected for partitioning is the smallest or largest element, one partition will have just 1 element, and the other partition will have all other elements. If this unequal partitioning happens at every level, there will be N - 1 levels, yielding (N - 1) * N, which is O(N2). - So the worst case runtime for the quicksort algorithm is O(N2). fortunately, this worst case runtime rarely occurs.

9.1.7: Worst case quicksort runtime. Assume quicksort always chooses the smallest element as the pivot. 1 Given numbers = {7 4 2 25 19}, i = 0, and k = 4, what is contents of the low partition? Use curly braces in your answer. 2 How many partitioning "levels" of are required for a list of 5 elements? 3 How many partitioning "levels" are required for a list of 1024 elements? 4 How many total comparisons are required to sort a list of 1024 elements?

Solution : 1. {2} The low partition is {2}, and the high partition is {4 7 25 19} 2. 4 1st: {a} {b c d e} 2nd: {a} {b} {c d e} 3rd: {a} {b} {c} {d e} 4th: {a} {b} {c} {d} {e} 3. 1023 In the worst case, for N elements, there will be N - 1 levels. 4. 1047552 (1024 - 1) * 1024 = 1023 * 1024 = 1047552 comparisons.

9.1.2: Quicksort pivot location and value. Determine the midpoint and pivot values. 1 numbers = {1 2 3 4 5}, i = 0, k = 4 midpoint = 2 numbers = {1 2 3 4 5}, i = 0, k = 4 pivot = 3 numbers = {200 11 38 9}, i = 0, k = 3 midpoint = 4 numbers = {200 11 38 9}, i = 0, k = 3 pivot = 5 numbers = {55 7 81 26 0 34 68 125}, i = 3, k = 7 midpoint = 6 numbers = {55 7 81 26 0 34 68 125}, i = 3, k = 7 pivot =

Solution: 1. 2 midpoint = i+ ((k−i)/2) =0+(4−0)/2 = 4 /2 =2 The midpoint determines the middle element of the array. 2. 3 midpoint = 0 + (4 - 0)/2 = 2 The midpoint is 2, so the pivot value is the element at numbers[2]. 3. 1 midpoint=i+ ((k−i)/2) = 0+(3-0)/2 =3/2 =1 If the number of elements in the numbers array is even, the midpoint value is rounded down. 4. 11 midpoint = 0 + (3 - 0)/2 = 1 The midpoint is 1, so the pivot value is the element at numbers[1]. 5. 5 midpoint=i+ ((k−i)/2) = 3+(7-3)/2 =3/2 =5 The midpoint between index 3 and index 7 is located at 5. The partition function (or method) can be applied to a subset of the elements. 6. 34 midpoint = 3 + (7 - 3)/2 = 5 The midpoint of index 3 and index 7 is 5, so the pivot value is the element at numbers[5].

9.1.6: Quicksort runtime. Assume quicksort always chooses a pivot that divides the elements into two equal parts. 1 How many partitioning levels are required for a list of 8 elements? 2 How many partitioning "levels" are required for a list of 1024 elements? 3 How many total comparisons are required to sort a list of 1024 elements?

Solutions: 1. 3 1st: {a b c d} {e f g h} 2nd: {a b} {c d} {e f} {g h} 3rd: {a} {b} {c} {d} {e} {f} {g} {h} 2. 10 log2 1024 = 10. Assuming equal-size partitions, for N elements, the number of levels is log2 N. 3. 10240 log2 1024 * 1024 = 10 * 1024 = 10240 comparisons. There will be log2 N partitioning levels, and each level requires N comparisons.

quick sort comparison

The quicksort algorithm's runtime is typically O(N log N). Quicksort has several partitioning levels , the first level dividing the input into 2 parts, the second into 4 parts, the third into 8 parts, etc. At each level, the algorithm does at most N comparisons moving the l and h indices. If the pivot yields two equal-sized parts, then there will be log N levels, requiring the N * log N comparisons.

After portion

Once partitioned, each partition needs to be sorted. Quicksort is typically implemented as a recursive algorithm using calls to quicksort to sort the low and high partitions. This recursive sorting process continues until a partition has one or zero elements, and thus already sorted.

9.1.3: Low and high partitions. Determine if the low and high partitions are correct given h and pivot. # Question Your answer 1. pivot = 35 2. pivot = 65

Solution: 1. true Elements in the low partition are ≤ 35: {1 4 35} Elements in the high partition are ≥ 35: {62 98} 2. true Elements in the low partition are ≤ 65: {29 17 65 39} Elements in the high partition are ≥ 65: {84} Partitions may not be sorted.

partition algorithm

The partitioning algorithm uses two index variables l and h (low and high), initialized to the left and right sides of the current elements being sorted. As long as the value at index l is less than the pivot value, the algorithm increments l, because the element should remain in the low partition. Likewise, as long as the value at index h is greater than the pivot value, the algorithm decrements h, because the element should remain in the high partition. Then, if l >= h, all elements have been partitioned, and the partitioning algorithm returns h, which is the index of the last element in the low partition. Otherwise, the elements at indices l and h are swapped to move those elements to the correct partitions. The algorithm then increments l, decrements h, and repeats.


Ensembles d'études connexes

U50: The Industrial Age: Practice quiz

View Set

Psychology Module #17: Adulthood

View Set

Ch. 49: Assessment and Management of Patients with Hepatic Disorders

View Set

Indian Subcontinent Countries & Capitals

View Set