QUICK SORT ALGORITHM IN C AND C++
If you want to learn another sorting algorithm, visit the webpage: Sorting arrays
| The sorting algorithm that proved to be the fastest. It consists of a recursive repetition of the separation of the elements of an array around some value that has been adopted as "middle value". This element is called PIVOT and there is few different ways to choosing it. In this text, the last element of subarray in the current iteration is choosed for PIVOT. If e.g. performs sorting of the array in ascending order, passing through the array, from left to right, separates so after the end of the iteration on the left side of PIVOT there are values which are less than or equal to PIVOT, and on the right are larger values. The process is repeated until all elements up to PIVOT are examined. In the end, PIVOT will be puting in the right place, ie. in place after the elements arranged on the left, those whose values were less than PIVOT. Let's we consider e.g. the following unsorted array: |
VIDEO: Quick-sort, algorithm |
Figure 1: Quick sort in C. Initial array
For PIVOT, the element at the end of the array was adopted, ie. at position 9, whose value is 14. Orange frame indicate the current element of the array whose value we compare with PIVOT. The figure above shows the situation in the second cycle. This value is compared to the pivot and if it is smaller, it is replaced with an element framed by a dashed red line. The red box actually shows the first next position to the right of the last replaced element. On the left side on the all positions elements must be smaller than PIVOT, in this case less than 14.So, if there is a replacement of the current element (orange) and the element (red), the position for the next possible replacement is moved one place to the right. If the current element is less than the pivot, there is no replacement, but the next element to the right is taken, ie the orange frame is moved. In the first cycle, the number 25 was compared with 14, considering that it is already there, there was no replacement. In the second cycle, which is shown in the previous picture, the number 13 is compared with PIVOT, and since it is smaller, the numbers 13 and 25 will be replaced, that is. numbers in positions 0 and 1 (see next picture).
Figure 2: Quick sort. Replacement of elements
Element 13 is then located at the beginning of the array, and the next position for eventual replacement is to the right at position no. 1. In the next cycle, the number 10 is compared with 14 (PIVOT), and then -2. Both of these values are less than 14, so they will also be replaced and located on the left side of the position for next replacement, which is now moved to position 3 (Red rectangle in Figure 3). The next member of the array, 16 is greater than 14 so just it is moved on to the next element in position 5, see Figure 3:
Figure 3: Quick sort. Moving to next position
At the end of the observed iteration, when all elements of the array to the left of PIVOT compared, the PIVOT is placed in the appropriate place to the right of all moved members on the left. This position is marked with a red rectangle, see Figure 4:
Figure 4: Quick sort. Moving PIVOT
The code that performs the separations shown above is shown using the function shown below:C, C++
/*A method that separates the elements of an array. The array is passed to the method as a parameter a [] . The first left position of the observed array(subarray) "l" and the last right position "r" also are passed.*/
void elementSeparation(int a[ ], int l,int r)
{
void elementSeparation(int a[ ], int l,int r)
{
int p = a[ r ];// Pivot is element at last position
int i, j;
i=l-1;
for(j=l; j<=r-1; j++)
{
}int i, j;
i=l-1;
for(j=l; j<=r-1; j++)
{
if(a[j] < p)
{
}{
if(i != j)
{
}{
swap(&a[i+1],&a[j]);
i++;
}i++;
