# Quick sorting algorithm

Keywords: Algorithm data structure

Quicksort is an improvement of bubble sort algorithm.

## 1. Sorting process

The quick sort algorithm realizes sorting through multiple comparisons and exchanges. The sorting process is as follows:
(1) First, set a boundary value, and divide the array into left and right parts through the boundary value.
(2) Collect data greater than or equal to the boundary value to the right of the array, and data less than the boundary value to the left of the array. At this time, all elements in the left part are less than or equal to the boundary value, while all elements in the right part are greater than or equal to the boundary value.
(3) Then, the data on the left and right can be sorted independently. For the array data on the left, you can take another boundary value and divide this part of the data into left and right parts. Similarly, place the smaller value on the left and the larger value on the right. The array data on the right can also be processed similarly.
(4) By repeating the above process, we can see that this is a recursive definition. After the left part is sorted recursively, the right part is sorted recursively. When the sorting of the data in the left and right parts is completed, the sorting of the whole array is completed.

## 2. Sorting steps

### Sorting principle

Let the array to be sorted be A[0]... A[N-1], first select any data (usually
Use the first number of the array) as the key data, and then put all smaller numbers to its left and all larger numbers to its right. This process is called a quick sort. It is worth noting that quick sorting is not a stable sorting algorithm, that is, the relative positions of multiple identical values may change at the end of the algorithm.
The quick sort algorithm is:
1) Set two variables, i and j, at the beginning of sorting: i=0, j=N-1;
2) Take the first array element as the key data and assign it to key, that is, key=A[0];
3) Start the forward search from j, that is, start the forward search from the back (j –), find the first value A[j] less than the key, and exchange the values of A[j] and A[i];
4) Start the backward search from I, that is, start the backward search from the front (I + +), find the first A[i] greater than the key, and exchange the values of A[i] and A[j];
5) Repeat steps 3 and 4 until ij; (in steps 3 and 4, no qualified value is found, that is, when A[j] in 3 is not less than key and A[i] in 4 is not greater than key, change the values of J and I so that j=j-1 and i=i+1 until it is found. When the qualified value is found and exchanged, the position of I and j pointers remains unchanged. In addition, ij must be when I + or J - is completed, and the cycle ends.).

### Sorting demonstration

Suppose the initial sequence {xi} is: 5, 3, 7, 6, 4, 1, 0, 2, 9, 10, 8.
At this time, ref=5, i=1, j=10. Look from back to front. The first number smaller than 5 is x8=2, so the sequence is: 2, 3, 7, 6, 4, 1, 0, 5, 9, 10, 8.
At this time, i=1, j=8. Look from front to back. The first number greater than 5 is x3=7, so the sequence is: 2, 3, 5, 6, 4, 1, 0, 7, 9, 10, 8.
At this time, i=3, j=8, look forward from the 8th bit, and the first number smaller than 5 is x7=0, so: 2, 3, 0, 6, 4, 1, 5, 7, 9, 10, 8.
At this time, i=3, j=7. Look back from the third place. The first number greater than 5 is x4=6, so: 2, 3, 0, 5, 4, 1, 6, 7, 9, 10, 8.
At this time, i=4, j=7, look forward from the 7th place, and the first number smaller than 5 is x6=1, so: 2, 3, 0, 1, 4, 5, 6, 7, 9, 10, 8.
At this time, i=4, j=6, look back from the fourth place, and there is no number greater than 5 until the sixth place. At this time, i=j=6, ref becomes a dividing line. The previous numbers are smaller than it, and the subsequent numbers are larger than it. The same method can be used to sort the front and rear scores.

## 3. Sample code

### C language

```#include<stdio.h>
void quick_sort(int array[],int left,int right)
{
int i=left,j=right;
int temp;
int pivot;  //Datum point

pivot = array[(left + right)/2];

while(i <= j)
{
//Find elements greater than or equal to the reference point from left to right
while(array[i] < pivot)
{
i++;
}
//Find elements less than or equal to the reference point from right to left
while(array[j] > pivot)
{
j--;
}
//If I < = J, swap
if(i <= j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}

if(left < j)
{
quick_sort(array,left,j);
}
if(right > i)
{
quick_sort(array,i,right);
}

}

int main()
{
int array[]={9,4,7,2,5,1,3,6,8,0};
int i,length;

length = sizeof(array) / sizeof(array[0]);
quick_sort(array,0,length - 1);
printf("The ordered sequence is:");
for(i=0;i<length;i++)
{
printf("%d ",array[i]);
}
printf("\n");
return 0;
}
```

### java version

```public static int[] qsort(int arr[],int start,int end) {
int pivot = arr[start];
int i = start;
int j = end;
while (i<j) {
while ((i<j)&&(arr[j]>pivot)) {
j--;
}
while ((i<j)&&(arr[i]<pivot)) {
i++;
}
if ((arr[i]==arr[j])&&(i<j)) {
i++;
} else {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
if (i-1>start) arr=qsort(arr,start,i-1);
if (j+1<end) arr=qsort(arr,j+1,end);
return (arr);
}

public static void main(String[] args) {
int arr[] = new int[]{3,3,3,7,9,122344,4656,34,34,4656,5,6,7,8,9,343,57765,23,12321};
int len = arr.length-1;
arr=qsort(arr,0,len);
for (int i:arr) {
System.out.print(i+"\t");
}
}

/*//Mode II*/
More efficient code:(TextendsComparable and SortUtil Are their own encapsulated classes, which are rewritten and implemented compareTo and swap method)
public <TextendsComparable<?superT>>
T[] quickSort(T[] targetArr,int start,int end)
{
inti=start+1,j=end;
T key=targetArr[start];
SortUtil<T> sUtil=new SortUtil<T>();

if(start==end)return(targetArr);

/*Search for values that do not meet the conditions from i + + and j -- and exchange them
*
*The conditions are: the i + + direction is less than the key, and the j -- direction is greater than the key
*/
while(true)
{
while(targetArr[j].compareTo(key)>0)j--;
while(targetArr[i].compareTo(key)<0&&i<j)i++;
if(i>=j)break;
sUtil.swap(targetArr,i,j);
if(targetArr[i]==key)
{
j--;
}else{
i++;
}
}

/*Key data in the middle*/
sUtil.swap(targetArr,start,j);

if(start<i-1)
{
this.quickSort(targetArr,start,i-1);
}
if(j+1<end)
{
this.quickSort(targetArr,j+1,end);
}

returntargetArr;
}

/*//Mode 3: reduce the number of exchanges and improve efficiency/*/
private<TextendsComparable<?superT>>
voidquickSort(T[]targetArr,intstart,intend)
{
inti=start,j=end;
Tkey=targetArr[start];

while(i<j)
{
/*Traverse the target array in the j -- direction until the value is smaller than the key*/
while(j>i&&targetArr[j].compareTo(key)>=0)
{
j--;
}
if(i<j)
{
/*targetArr[i]It has been saved in the key. You can fill in the following numbers*/
targetArr[i]=targetArr[j];
i++;
}
/*Traverse the target array in the i + + direction until the value is greater than the key*/
while(i<j&&targetArr[i].compareTo(key)<=0)
/*Here, it must be less than or equal to zero. Assuming that there are 100 million 1 and 0 in the array alternately, and the value of the key happens to be 1, the effect of less than or equal will reduce the execution of the following if statement by 100 million times.*/
{
i++;
}
if(i<j)
{
/*targetArr[j]It has been saved in targetArr[i]. You can fill in the previous value*/
targetArr[j]=targetArr[i];
j--;
}
}
/*i==j*/