Fast sorting is one of the more difficult to understand the beginner of a few algorithms, here can be simplified to explain, hoping to help everyone.
Quick Sort Basic steps:
- Select an element from the series, called the "Datum" (pivot).
- Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the datum (the same number can be on either side). At the end of this partition, the datum is in the middle of the sequence. This is called partition (partition) operation.
- recursively (recursive) sorts sub-columns that are smaller than the base value elements and sub-columns that are larger than the base value elements.
The picture below will help you understand. The selected pivot is indicated in blue:
In principle, you can select any element as the benchmark.
# choose pivot
swap a[1,rand(1,n)]
# 2-way Partition
K = 1
For i = 2:n, if a[i] < a[1], swap a[++k,i]
Swap A[1,k]
→invariant:a[1..k-1] < A[k] <= A[K+1..N]
# recursive sorts
Sort A[1..k-1]
Sort A[k+1,n]
We use the array arr[] = {2, 6, 4, 10, 8, 1, 9, 5, 11, 7} for an example.
Select the last element as pivot.
- PIVOT = 7 or arr[10]
- Iterate over the data of area 0-9, put pivot aside
- Starting from the left, I is the index (array subscript)
- if (2 < pivot) = move ahead
- if (6 < pivot) = move ahead
- if (4 < pivot) = move ahead
- if (< pivot) = NO, STOP (i points to and i = 3)
- Change direction, start on the right, J for index (array subscript)
- if (one > Pivot) = move towards left
- if (5 > Pivot) = NO, STOP. (J points to 5 and j = 7)
- If (i<j), swap arr[i] and arr[j]
Array now becomes {2, 6, 4, 5, 8, 1, 9, 10, 11, 7}
- Increment I and Decrease J.
- Again We start i from 8 and J from 9.
- We'll get the array as:-{2, 6, 4, 5, 1, 8, 9, ten, one, 7} where (i = 5 and J = 4)
- Swap the pivot element with Arr[i].
- Thus we have {2, 6, 4, 5, 1, 7, 9, ten, one, 8}
When pivot = 7, all the numbers smaller than 7 are on the left of 7, the number is greater than 7 on the right, and the data on the left and right parts are sorted in the same order until the entire array is ordered.
Here is the algorithm implementation:
#include <stdio.h>//a simple function to swap numbersvoidSwapint(Inint*j) { inttemp = *i; *i = *J; *j =temp;} //a function to partition the array arr//Having starting index As-start//and ending index as-endintPartitionintArr[],intStartintend) { //we take the pivot to being the last element//That's means all elements smaller//To it'll be on left and larger on right intPivot =Arr[end]; //taking I and J to define the range, we leave the pivot inti =start; intj = end-1; //loop till in range while(i<=j) {//keep moving till the left element is smaller than pivot while(arr[i]<pivot) I++; //Keep moving left till right element is larger while(arr[j]>pivot) J--; //we need to swaps the left and right if(i<=j) {Swap (&arr[i],&Arr[j]); I++; J--; } } //once partitioned, we need to put the pivot at correct placeSwap (&arr[i],&Arr[end]); //return the position of pivot returni;} voidPerformquicksort (intArr[],intStartintend) { //The terminating condition for recursion if(start<end) { //get the partition index intp =partition (arr, start, end); //perform quick sort on left sub partPerformquicksort (arr, start, p1); //perform quick sort on right sub partPerformquicksort (arr, p+1, end); }} //defining a function to perform merge sort on array arr[] of given sizevoidQuickSort (intArr[],intsize) {Performquicksort (arr,0, size-1);} //Driver Program to test the above functionintMainvoid){ inti; intarr[Ten] = {2,6,4,Ten,8,1,9,5,3,7}; QuickSort (arr,Ten); printf ("SORTED array:-"); for(i=0;i<Ten; i++) printf ("%d", Arr[i]); return 0;}
Quick sorting (Quick sort)