# quick sort

Posted on July 12, 2013 algorithms- Sorting algorithm developed by Tony Hoare.
- O(n log n) comparisons to sort n items.
- Worst case: O(n^2)
- This is a divide and conquer algorithm.
- It is more efficient that other algorithms as the input size grows.

## steps

- pick a pivot. (random works well, finding the median is best)
- split list into two.
- items lower than the pivot value go in one list.
- items greater than the pivot value go in the other list.

- recursively apply the above steps to each of the sublists.

The base case for the recursion is an array of size 0 or 1.

## notes

The efficiency of the algorithm relies on finding an appropriate pivot value. The more balanced the two sublists are, the more efficient the divide and conquer will work.

The above example creates a new array for each of the sub lists. An in-place version can be more efficient because it just re-arranges elements in the source list. This reduces the overall memory footprint. Another thing to note is that ruby arrays dynamically expand, so there is no need to specify an array size for each declared array.

One of the key differences between quick sort and merge sort is that quick sort does the sorting as it divides the list into sublists. Merge sort does the sorting during the merge step.