Quick sort is a commonly used sorting method.
The idea of quick sort is:
First select a reference value in the array. The function of this reference value is to divide the entire array into two parts. All values less than this reference value are on the left of the reference value, and all values greater than this reference value are on the right of the reference value.
Less than the reference value | Reference | Part greater than reference value |
Then use the same method for the part less than the reference value and the part greater than the reference value. Until this part can no longer be divided. This is the algorithm of quick sort.
Example. Given an integer array, use the fast method to sort.
Next, start quick sorting. The default is ascending sort.
The following is the original data, there are 10 elements in total. Recorded as:
int originalData[10];
The contents of the array are shown below.
1 | 10 | 3 | -12 | 51 | 23 | 1 | 4 | 12 | 4 |
first round:
1. Select a reference value and record it as
int standardValue;
Here we take the element at the first position as the reference value, then
standardValue = 1;
2. Two subscripts are required to access the element. One is called the left cursor, which is initialized to 0; the other is called the right cursor, which is initialized to 9 (array length -1);
Recorded as:
Left cursor
int leftIndex = 0;
Right cursor
int rightIndex = 9;
3. Let the right cursor move from right to left, and find a value smaller than the reference value standardValue during the movement. When such a value is found, at this time
rightIndex = 3;
originalData[rightIndex] = -12 < standardValue = 1;
4. Put the value of the position indicated by the right cursor on the position indicated by the left cursor; namely
originalData[leftIndex] = originalData[rightIndex];
The array situation is as follows:
standardValue = 1;
leftIndex = 0;
rightIndex = 3;
The contents of the array are shown below.
-12 | 10 | 3 | -12 | 51 | 23 | 1 | 4 | 12 | 4 |
5. Let the left cursor move from left to right, and find a value larger than the reference value standardValue during the movement. When such a value is found, at this time
leftIndex = 1;
originalData[leftIndex] = 10 > standardValue = 1;
6. Put the value of the position indicated by the left cursor on the position indicated by the right cursor; namely
originalData[rightIndex] = originalData[leftIndex];
The array situation is as follows:
standardValue = 1;
leftIndex = 1;
rightIndex = 3;
-12 | 10 | 3 | 10 | 51 | 23 | 1 | 4 | 12 | 4 |
7. Let the right cursor move from right to left, and find a value smaller than the reference value standardValue during the movement. The result was not found.
when
leftIndex = rightIndex
When, this round of sorting ends. at this time
leftIndex = rightIndex = 1;
-12 | 10 | 3 | 10 | 51 | 23 | 1 | 4 | 12 | 4 |
Need to put the reference value back into the array. which is
originalData[leftIndex] = standardValue = 1;
At this time, the contents of the array are as follows,
-12 | 1 | 3 | 10 | 51 | 23 | 1 | 4 | 12 | 4 |
second round:
Using element value 1 as the line, divide the array into two parts. The left part is less than or equal to 1, and the right part is greater than or equal to 1.
-12 | 1 | 3 | 10 | 51 | 23 | 1 | 4 | 12 | 4 |
Treat the sub-array on the left side of the array (the yellow part) as a new array and perform the same operation.
-12 |
The sub-array on the left is already ordered.
Treat the sub-array on the right side of the array (the blue part) as a new array and perform the same operation.
3 | 10 | 51 | 23 | 1 | 4 | 12 | 4 |
Under the same procedure, you will get the result as shown below.
1 | 3 | 51 | 23 | 10 | 4 | 12 | 4 |
The third round:
Using element value 3 as the line, divide the array into two parts. The left part is less than or equal to 3, and the right part is greater than or equal to 3.
Treat the sub-array on the left side of the array (the yellow part) as a new array and perform the same operation.
1 |
Treat the sub-array on the right side of the array (the blue part) as a new array and perform the same operation.
51 | 23 | 10 | 4 | 12 | 4 |
Under the same procedure, you will get the result as shown below.
4 | 23 | 10 | 4 | 12 | 51 |
Fourth round:
Using element value 51 as a line, divide the array into two parts. The left part is less than or equal to 51, and the right part is greater than or equal to 51.
Now only the left part is left.
Treat the sub-array on the left side of the array (the yellow part) as a new array and perform the same operation.
4 | 23 | 10 | 4 | 12 |
Under the same procedure, you will get the result as shown below.
4 | 23 | 10 | 4 | 12 |
Fifth round:
Using element value 4 as the line, divide the array into two parts. The left part is less than or equal to 4, and the right part is greater than or equal to 4.
Now only the right part is left.
23 | 10 | 4 | 12 |
Under the same procedure, you will get the result as shown below.
12 | 10 | 4 | 23 |
Sixth round:
Using the element value 23 as the line, divide the array into two parts. The left part is less than or equal to 23, and the right part is greater than or equal to 23.
Now only the left part is left.
12 | 10 | 4 |
Under the same procedure, you will get the result as shown below.
4 | 10 | 12 |
Seventh round:
Using element value 12 as the line, divide the array into two parts. The left part is less than or equal to 12, and the right part is greater than or equal to 12.
Now only the left part is left.
4 | 10 |
The remaining array is now in order.
And all the elements are already in order, so the task is over.
Put these sub-arrays together in order of position to get the following array.
-12 | 1 | 1 | 3 | 4 | 4 | 10 | 12 | 23 | 51 |
The original array is:
-12 | 10 | 3 | 10 | 51 | 23 | 1 | 4 | 12 | 4 |
The sorting got the correct ascending result. (Equal values do not change the relative position, so it is a stable sorting algorithm.)
The C++ code implementation is given below, and the source code is for reference only.
#include <iostream>
using namespace std;
// 前向声明.
void quickSort(int *originalArray, int leftIndex, int rightIndex);
int main()
{
// 整数的个数.
int numbers = 0;
cin >> numbers;
// 保存整数的数组.
int *originalData = new int[numbers];
// 从标准输入中读取数据.
for (int i = 0; i < numbers; ++i)
{
cin >> originalData[i];
}
// 对数据进行排序.
// 使用二分排序法.
quickSort(originalData, 0, numbers - 1);
// 输出最小值和最大值.
cout << originalData[0] << " " << originalData[numbers - 1] << endl;
return 0;
}
void quickSort(int *originalArray, int leftIndex, int rightIndex)
{
// leftIndex 的备份.
int backLeft = leftIndex;
// rightIndex 的备份.
int backRight = rightIndex;
// 递归结束条件.
if (leftIndex > rightIndex)
{
// nothing to do.
}
else
{
// 参考标准.
int standardValue = originalArray[leftIndex];
while (leftIndex < rightIndex)
{
// 在 middleIndex 的右边部分,从后往前找到一个比 middleIndex 元素值小的元素.
while (leftIndex < rightIndex)
{
if (originalArray[rightIndex] >= standardValue)
{
// next element.
--rightIndex;
}
else
{
break;
}
}
originalArray[leftIndex] = originalArray[rightIndex];
// 在 middleIndex 的左边部分,从前往后找到一个比 middleIndex 元素值大的元素.
while (leftIndex < rightIndex)
{
if (originalArray[leftIndex] <= standardValue)
{
// next element.
++leftIndex;
}
else
{
break;
}
}
originalArray[rightIndex] = originalArray[leftIndex];
}
// 保存标准值.
originalArray[leftIndex] = standardValue;
// 递归调用自身.
quickSort(originalArray, backLeft, leftIndex - 1);
quickSort(originalArray, leftIndex + 1, backRight);
}
}
The following is the Java code, the source code is for reference only.
import java.util.Arrays;
import java.util.Scanner;
public class QuickSort
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int numbers = Integer.parseInt(scanner.nextLine());
int[] originalArray = new int[numbers];
for (int i = 0; i < numbers; ++i)
{
originalArray[i] = scanner.nextInt();
}
quickSort(originalArray, 0, numbers - 1);
System.out.println(Arrays.toString(originalArray));
scanner.close();
}
public static void quickSort(int[] originalData, int leftIndex, int rightIndex)
{
int backLeft = leftIndex;
int backRight = rightIndex;
if (leftIndex > rightIndex)
{
// nothing to do.
}
else
{
int standardValue = originalData[leftIndex];
while (leftIndex < rightIndex)
{
while (leftIndex < rightIndex)
{
if (originalData[rightIndex] >= standardValue)
{
// next element.
--rightIndex;
}
else
{
break;
}
}
originalData[leftIndex] = originalData[rightIndex];
while (leftIndex < rightIndex)
{
if (originalData[leftIndex] <= standardValue)
{
// next element.
++leftIndex;
}
else
{
break;
}
}
originalData[rightIndex] = originalData[leftIndex];
}
originalData[leftIndex] = standardValue;
// 递归调用自身.
quickSort(originalData, backLeft, leftIndex - 1);
quickSort(originalData, leftIndex + 1, backRight);
}
}
}
Over!