Fast sorting is the upgrade of bubble sorting, which belongs to exchange sorting.

The basic idea of fast sorting is: the records to be sorted are divided into two parts by one-time sorting, and each keyword of one part is smaller than that of the other part. Then the two parts of records can be sorted separately, and the whole sequence is in order.

The code is as follows:

void QuickSort(vector<int> &seq, int low, int high) { int pivot; if (low < high) //As long as the low is less than the high, the recursive sorting will be carried out all the time { pivot = Partition(seq, low, high); QuickSort(seq, low, pivot - 1); QuickSort(seq, pivot + 1, high); } return; }

In this part, the Partition function is used to find out the index position of the keyword that can divide the sequence into two parts. The sequence elements in front of the keyword are smaller than it, and the elements in the back are larger than it. Then, the same operation is completed by recursion between the sequence in front and the sequence in the back to ensure the sequence in order.

int Partition(vector<int> &vec, int low, int high) { int pivotkey; pivotkey = vec[low]; int tmp = pivotkey; while (low < high) { while (low < high&&vec[high] >= pivotkey) { high--; } //swap(vec[low], vec[high]); vec[low] = vec[high];//Using direct replacement in this part can save the time of exchange while (low < high&&vec[low] <= pivotkey) { low++; } //swap(vec[low], vec[high]); vec[high] = vec[low]; } vec[low] = tmp; return low; }

In the above function, first set the first element as a hub, and then start the continuous comparison from both ends. Finally, put the hub element in a proper position to ensure that the front element is smaller than it and the back element is larger than it, and return the last position of the hub element.

The above-mentioned optimization strategy is to store the hub elements temporarily, and then change the swap operation to directly replace them when it is found that the location of the elements is not appropriate, and finally directly put the hub elements temporarily stored in the last place where they need to stay.

The complete code is as follows:

int Partition(vector<int> &vec, int low, int high) { int pivotkey; pivotkey = vec[low]; int tmp = pivotkey; while (low < high) { while (low < high&&vec[high] >= pivotkey) { high--; } //swap(vec[low], vec[high]); vec[low] = vec[high];//Using direct replacement in this part can save the time of exchange while (low < high&&vec[low] <= pivotkey) { low++; } //swap(vec[low], vec[high]); vec[high] = vec[low]; } vec[low] = tmp; return low; } void QuickSort(vector<int> &seq, int low, int high) { int pivot; if (low < high) //As long as the low is less than the high, the recursive sorting will be carried out all the time { pivot = Partition(seq, low, high); QuickSort(seq, low, pivot - 1); QuickSort(seq, pivot + 1, high); } return; } int main() { vector<int> vec{ 1, 4, 7, 2, 5, 8, 3, 6, 9 }; //vector<int> vec{ 1,5,9,7,5,3,4,8,6,2,11,55,48,35,68,79,26,21,10 }; QuickSort(vec, 0, vec.size() - 1); for (auto i : vec) { cout << i << " "; } system("pause"); return 0; }

Output verification: