This example for you to share the C + + quick sort algorithm, for your reference, the specific content is as follows

**1、 The basic idea is as follows:**

Through one-step sorting, the data to be sorted is divided into two independent parts. All the data of one part is smaller than that of the other part. Then the two parts of data are sorted quickly according to this method. The whole sorting process can be carried out recursively, so as to make the whole data into an ordered sequence.

**2、 Method 1: implementation program****Scan left and right**

```
//Sort the entire object according to the first selection code
//The sequence is divided into left and right word sequences
//The sort codes of all objects in the left subsequence are less than or equal to the sort codes of benchmark objects;
//The sort codes of all objects in the right subsequence are greater than those of the reference object;
//The datum object is in the middle of the two subsequences, which is also the final position of the object
//Then repeat the above method for the two subsequences until all the objects are in the corresponding position
#include <iostream>
//A quick sort algorithm
int quickPass(int arr[], int low, int high) {
int down, up, temp;
down = low;
up = high;
temp = arr[low];
while(down < up) {
While ((down < up) & arr [up] > = temp) // scan from right to left
up--;
if(down < up)
Arr [down + +] = arr [up]; // fill in arr [up] in the vacated cell (pointed by down)
While ((down < up) & arr [down] < temp) // scan from left to right
down++;
if(down < up)
Arr [up --] = arr [down]; // fill in arr [down] in the vacated cell (pointed by up)
}
Arr [down] = temp; // finally, insert the benchmark back into the middle of the array
return down;
}
//Quick sort algorithm
void quickSort(int arr[], int low, int high) {
//Quick sorting of sequences from arr [low] to arr [high]
int mid;
if(low < high) {
Mid = QuickPass (arr, low, high); // do a quick sort from arr [low] to arr [high]
Quicksort (arr, low, mid-1); // recurses the left half
Quicksort (arr, mid + 1, high); // recursively process the right half
}
}
int main(int argc, const char * argv[]) {
// insert code here...
int len, i;
int arr[] = {40, 30, 60, 90, 70, 10, 20, 40};
len = sizeof(arr) / sizeof(arr[0]);
STD:: cout < "before sorting":;
for(i = 0; i < len; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
Quicksort (arr, 0, len-1); // call quicksort
STD:: cout < "after sorting":;
for(i = 0; i < len; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
return 0;
}
```

Results of operation:

**3、 Method 2****Just scan to one side**

```
//Another quick sort method: scan only to one side
#include <iostream>
//Two commutative numbers
void Exchange(int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
//The sequence is divided into two parts, the left part is smaller and the right part is larger
int Partition(int arr[], int low, int high) {
int x, i, j;
X = arr [high]; // take the number of high position as the benchmark
i = low - 1;
//Data classification: the left part is smaller and the right part is larger
for(j = low; j < high; j++)
If (arr [J] < = x) {// scan to the right to find the number less than or equal to the reference
i = i + 1;
Exchange (arr [i], arr [J]); // exchange
}
Exchange (arr [i + 1], arr [high]); // put the benchmark in the middle
return i+1;
}
//Quick sort algorithm
void quickSort(int arr[], int low, int high) {
int q;
if(low < high) {
q = Partition(arr, low, high);
quickSort(arr, low, q-1);
quickSort(arr, q+1, high);
}
}
int main(int argc, const char * argv[]) {
int len, i;
int arr[] = {40, 30, 60, 90, 70, 10, 20, 40};
len = sizeof(arr) / sizeof(arr[0]);
STD:: cout < "before sorting":;
for(i = 0; i < len; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
Quicksort (arr, 0, len-1); // call quicksort
STD:: cout < "after sorting":;
for(i = 0; i < len; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
return 0;
}
```

Results of operation:

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.