# Quick sort (single row, double row, three row)

Posted by latvaustin on Mon, 10 Jan 2022 22:47:17 +0100

# 1, What is quick sort

Quick sort (English Name: Quicksort, sometimes called partition exchange sort) is an efficient sorting algorithm, which was invented by Tony Hoare in 1959 (published in 1961). Quick sorting, for short, is an improvement on bubble sorting.

# 2, Single way fast platoon

## 1. Execution process and principle

(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. Code implementation

The code is as follows (example):

```private static void quickSortOneWay(int[] arr, int l, int r) {
if(l>=r){
return;
}
int p = PartitionOneWay(arr,l,r);
quickSortOneWay(arr,l,p-1);
quickSortOneWay(arr,p+1,r);
}
```
```private static int PartitionOneWay(int[] arr, int l, int r) {
swap(arr,l,(int)(Math.random()*(r-l+1)+l));
int v = arr[l];
int i = l;
int j = i+1;
while(j<=r){
if(arr[j]<v){
swap(arr,i+1,j);
i++;
}
j++;
}
swap(arr,l,i);
return i;

}
```

# 3, Dual fast platoon

## 1. Execution process and principle

The difference between a two-way fast platoon and a single way fast platoon is that it no longer goes all the way from left to right, but traverses the value less than V from left to right, and the value greater than V from right to left, which is divided into two ways.
For arrays with a large number of duplicate elements, the above quick sorting efficiency is very low, because in our judgment above, if the element is less than v, the element is placed in the < v part, and if the element is greater than or equal to v, the element is placed in the > v part. At this time, if there are a large number of duplicate elements in the array, the > v part will become very long, resulting in imbalance between the left and right sides and reduced performance.

## 2. Code implementation

The code is as follows (example)

```private static void quickSortTwoWay(int[] arr, int l, int r) {
if(l>=r){
return;
}
int p = partitionTwoWay(arr,l,r);
quickSortTwoWay(arr,l,p-1);
quickSortTwoWay(arr,p+1,r);
}
```
``` private static int partitionTwoWay(int[] arr, int l, int r) {
swap(arr,l,(int)(Math.random()*(r-l+1)+l));
int v = arr[l];
int i= l+1;
int j = r;
while(true){
while(i<=r&&arr[i]<v){
i++;
}
while(j>=l+1&&arr[j]>v){
j--;
}
if(i>j){
break;
}
swap(arr,i,j);
i++;
j--;
}
swap(arr,l,j);
return j;

}
```

# 3, Three way fast platoon

## 1. Execution process and principle

The three-way fast platoon here simply means that the soldiers are divided into three ways for quick sorting. The three ways are divided into those less than V, those equal to V and those greater than v.

## 2. Code implementation

The code is as follows (example)

```private static void quickSortThreeWay(int[] arr, int l, int r) {
if(l>=r){
return;
}
swap(arr,l,(int)(Math.random()*(r-l+1)+l));
int v = arr[l];
int lt = l;
int gt = r+1;
int i= lt+1;
while(i<gt){
if(arr[i]<v){
swap(arr,i,lt+1);
i++;
lt++;
}else if(arr[i]>v){
swap(arr,i,gt-1);
gt--;
}else{
i++;
}
}
swap(arr,l,lt);
quickSortThreeWay(arr,l,lt-1);
quickSortThreeWay(arr,gt,r);

}
```

# swap code

```private static void swap(int[] arr, int l, int r) {
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
```