(4) Quick sort of sorting algorithm

Ideas:

Quick sorting is different from the three sorting frameworks described earlier, regardless of sorted and unsorted. It is mainly to select a value, which is usually the first element, and then put the array elements smaller than this value on the left, and those larger than this value on the right. Regarding the left and right parts as two arrays, repeat the steps just now until they can no longer be divided.

Note : The operation of dividing the array elements into two parts is achieved by traversal comparison and exchange.
Detailed sorting process:
data to be sorted : 4 , 7 , 0, 2 , 8 , 1 (the first element is the selected value)
1, 7 , 0, 2 , 8 , 4 (traverse from back to front , encounter The number 1, which is less than 4, is exchanged)
1, 4 , 0, 2, 8 , 7 (traverse from front to back, and the number 7, which is greater than 4, is encountered, and exchanged)

Then continue, knowing that it is divided into two sub-arrays, the left is smaller than the right: 1
, 2 , 0 , 4 , 8, 7 (traverse from back to front, encounter the number 2 which is less than 4, and exchange) 1, 2, 0
, 4 , 8, 7 (traverse from front to back, no larger than 4, unchanged) The
above is a quick sorting process, continue to sort the two sub-arrays separately

Data to be sorted : 1 , 2, 0 (use the first element as the selected value)
0, 2, 1 (traverse from back to front, encounter a number 0 that is less than 1, and exchange)
0, 1 , 2 ( Traverse from the front to the back, no one is greater than 1)

Data to be sorted : 8 , 7 (use the first element as the selected value)
7, 8 (traverse from back to front , encounter a number 7, which is less than 8, and exchange)
7, 8 (traverse from front to back, no Encountered bigger than 8)

The last sort of result:
0,1,2,4,7,8

It can be seen from this that: quick sort is to continuously divide the array into two left and right sub-arrays. The left side of these two arrays is smaller than the right side. When the division is the smallest, an ordered array can be obtained.

The implementation code is as follows:

#include<iostream>
#include<vector>
int main()
{
    
    
	vector<int> A = {
    
     4,7,0,2,8,1 };
	quickSort(A, 0, A.size() - 1);
	return 0;
}
void quickSort(vector<int> &nums, int i, int j) // i,j 是排序的数组开始和结束索引
{
    
    
	if (i >= j) {
    
    	//边界条件,最小时(只有一个元素)返回
		return;
	}
	int key = i, temp, oldKey;
	while (true) {
    
    
		oldKey = key;
		for (int k =j; k > key; k--) {
    
    	// 从后往前遍历
			if (nums[k] < nums[key]) {
    
    	// 遇到比 选定值 小的交换
				temp = nums[k];
				nums[k] = nums[key];
				nums[key] = temp;
				key = k;
			}
		}
		for (int k = i; k < key; k++) {
    
    	// 从前往后遍历
			if (nums[k] > nums[key]) {
    
    	// 遇到比 选定值 大的交换
				temp = nums[k];
				nums[k] = nums[key];
				nums[key] = temp;
				key = k;
			}
		}
		if (oldKey == key) {
    
    	// 当两次遍历下来索引没有变动,说明已经完成一次排序,跳出循环
			break;
		}
	}
	quickSort(nums, i, key - 1);	// 递归,左数组排序(如上面的0,2,1)
	quickSort(nums, key + 1,j);		// 递归,右数组排序(如上面的8,7)
}

end:

If you want to know about other sorts, click here: Summary of Sorting Algorithms

Guess you like

Origin blog.csdn.net/h799710/article/details/107488065