22.QuickSort-21

22.QuickSort-21 - Quick Sort This is probably the most...

Info iconThis preview shows pages 1–3. 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
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
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

22.QuickSort-21 - Quick Sort This is probably the most...

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

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