You are on page 1of 4

Quicksort

Various algorithms exist for the sorting of a given list of numbers/strings. Some of the algorithms are quite simple such as bubble sort, while others are more complex. Each algorithm has its own pros and cons, and no single algorithm can be nominated as the best. Quicksort is one of the well known sorting algorithms and is the subject of this writeup. Problem: We are given a set of numbers that must be arranged in non-decreasing order. We need to use Quicksort for the solution. Note: In the text below, lg(n) means log of n to base 2. Solution #1: Basic Quicksort, recursive. Quicksort was developed by C.A.R. Hoare and is a widely used sorting technique, based on divide and conquer. It is an O(n*n) algorithm in the worst case, which is as bad as insertion and selection sorts. However, its popularity derives from the fact that it has an excellent average case behaviour of O(n*log(n)). In practice, Quicksort almost always achieves this behaviour. Quicksort first selects an element that is to be used as split-point from the list of given numbers. All the numbers smaller than the split-point are brought to one side of the list and the rest on the other. This operation is called splitting. Consider the list: 4 3 1 7 5 9 6 2 8 Now, assume the first element of the list, 4 as the split-point. Then, after splitting the list will be arranged as: 312475968 Or in some other order, say: 2 3 1 4 ... That is, the exact order is immaterial. What is important is that the order should be:
1. Elements less than split-point element. 2. Split-point element. 3. Elements greater than split-point element.

After this, the list is divided into lists, one containing the elements less than the splitpoint element and the other containing the elements more than the split-point element.

These two lists are again individually sorted using Quicksort, that is by again finding a split-point and dividing into two parts etc. In the program given below, we will let recursion sort the smaller lists. In the example, the two sub-lists, are: 2 3 1 and 7 5 9 6 8. Let us consider another example, this time in more detail.

The list to be sorted is shown at the top. The situation after each of the split operations is also shown, and for the first split - all the comparison-exchanges are shown in more detail. The number with a bold square around it is the split point and the part of the list shown in pink is the part thats being worked upon in that split operation. In the original list 53 is chosen as the split point. One by one each number is compared to the split point. If its greater, the list is left as is. If not, the splitpoint is moved one step to the right and the number in consideration exchanged with the splitpoint. At the end of the split operation, the splitpoint element is brought into the middle. Please make sure at this point you understand how the splitting is happening, based on the drawing above.

Features

Similar to mergesort - divide-and-conquer recursive algorithm One of the fastest sorting algorithms Average running time O(NlogN) Worst-case running time O(N2)

Basic idea
1. Pick one element in the array, which will be the pivot. 2. Make one pass through the array, called a partition step, re-arranging the entries so that: the pivot is in its proper place. entries smaller than the pivot are to the left of the pivot. entries larger than the pivot are to its right. 3. Recursively apply quicksort to the part of the array that is to the left of the pivot, and to the right part of the array.

Here we don't have the merge step, at the end all the elements are in the proper order.

Algorithm
STEP 1. Choosing the pivot Choosing the pivot is an essential step. Depending on the pivot the algorithm may run very fast, or in quadric time.: 1. Some fixed element: e.g. the first, the last, the one in the middle This is a bad choice - the pivot may turn to be the smallest or the largest element, then one of the partitions will be empty. 2. Randomly chosen (by random generator ) - still a bad choice. 3. The median of the array (if the array has N numbers, the median is the [N/2] largest number. This is difficult to compute - increases the complexity. 4. The median-of-three choice: take the first, the last and the middle element. Choose the median of these three elements. Example: 8, 3, 25, 6, 10, 17, 1, 2, 18, 5 The first element is 8, the middle - 10, the last - 5. The median of [8, 10, 5] is 8 STEP 2. Partitioning

Partitioning is illustrated on the above example. 1. The first action is to get the pivot out of the way - swap it with the last element 5, 3, 25, 6, 10, 17, 1, 2, 18, 8 2. We want larger elements to go to the right and smaller elements to go to the left. Two "fingers" are used to scan the elements from left to right and from right to left: [5, 3, 25, 6, 10, 17, 1, 2, 18, 8] ^ ^ i j

While i is to the left of j, we move i right, skipping all the elements less than the pivot. If an element is found greater then the pivot, i stops While j is to the right of i, we move j left, skipping all the elements greater than the pivot. If an element is found less then the pivot, j stops When both i and j have stopped, the elements are swapped. When i and j have crossed, no swap is performed, scanning stops, and the element pointed to by i is swapped with the pivot .

In the example the first swapping will be between 25 and 2, the second between 10 and 1. 3. Restore the pivot. After restoring the pivot we obtain the following partitioning into three groups: [5, 3, 2, 6, 1] [ 8 ] [10, 25, 18, 17] STEP 3. Recursively quicksort the left and the right parts

You might also like