QuickSort01 - Quick Sort This is probably the most common...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Quick Sort This is probably the most common sort used in practice, since it is usually the quickest in practice. It utilizes the idea of a partition (that can be done without an auxiliary array) with recursion to achieve this efficiency. Quick sort relies on the partition. Basically, a partition works like this: Given an array of n values, you must randomly pick an element in the array to partition by. Once you have picked this value, you must compare all of the rest of the elements to this value. If they are greater, put them to the “right” of the partition element, and if they are less, put them to the “left” of the partition element. When you are done with the partition, you KNOW that the partition element is in its CORRECTLY sorted location. In fact, after you partition an array, you are left with all the elements to the left of the partition element in the array, that still need to be sorted, and all of the elements to the right of the partition element in the array that also need to be sorted. And if you sort those two sides, the entire array will be sorted!
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Thus, we have a situation where we can use a partition to break down the sorting problem into two smaller sorting problems. Thus, the code for quick sort, at a real general level looks like: 1) Partition the array with respect to a random element. 2) Sort the left part of the array, using Quick Sort 3) Sort the right part of the array, using Quick Sort. Once again, since this is a recursive algorithm, we need a base case, that does not make recursive calls. (A terminating condition. ..) Our terminating condition will be sorting an array of one element. We know that array is already sorted. Here is an illustration of Quick Sort:
Background image of page 2
How to Partition in Place Consider the following list of values to be partitioned: 8 3 6 9 2 4 7 5 ^ ^ Let us assume for the time being that we are partition based on the last element in this array, 5. Here is how we will partition: Start two counters, one at array index 0 and the other at array index 6, (which is the second to last element in the array.) Advance the left counter forward until a value greater than the pivot is encountered. Advance the right counter backwards until a value less than the pivot is encountered. After these two steps have been performed, we have: 8 3 6 9 2 4 7 5 ^ ^ Now, swap these two elements, since we know that they are both on the "wrong" side: 4 3 6 9 2 8 7 5 ^ ^
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Now, continue to advance the counters as before: 4 3 6 9 2 8 7 5 ^ ^ Then swap as before: 4 3 2 9 6 8 7 5 ^ When both counters line up, swap the last element with the where the counter is to finish the partition:
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/09/2009.

Page1 / 11

QuickSort01 - Quick Sort This is probably the most common...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online