A brief explanation of the principle of sort() in C++ STL

Why do you want to write this Weibo? It is mainly found that in algorithm problems, many problems use sort() to sort the array, and then it will be much simpler to perform other operations on it. So let’s summarize the principle of sort(), and if there are any problems in the future, we can also trace them to the source and solve them. Moreover, sorting is also one of the important components in the algorithm, so research and research are beneficial and harmless. The usage method of sort() is sort(begin, end). In general programming, you can directly bring the begin() and end() functions of the container to traverse the container. Its functions are included in the header file <algorithm>, and its composition mainly includes two sorting methods (1) Insertion Sort (2) Quick Sort. A SORT_MAX variable is defined in STL to judge. If it is greater than SORT_MAX, the quick row is used, otherwise the insert row is used.

1. Insertion sort
The order of insertion sort is as follows, that is, in order, traverse from the first number to the back, find the position of the latter element in the former element, and then insert the number into it, so it is easy to know that the time complexity of insertion sort is O( n^2) power, its realization procedure is as follows:
void Insert_Sort(int *num,int n){
	int tmp=0;
	for(int i=0;i<n;i++){
		tmp=num[i];
		int j=i-1;
		while(j>=0&&tmp<num[j]){//Comparing the sorted array with the number itself, if it is larger than it, move the array backwards
			num[j+1]=num[j];
			j--;
		}
		num[j+1]=tmp;
	}
}

2 Quick sort:
Quick sort is an improvement on the method of bubble sort. Bubble sort is to exchange two adjacent elements. On this basis, quick sort uses the binary method to operate on the array to determine the position of a cardinality. , and then the numbers on the right side of the radix are larger than the radix, and the numbers on the left side are smaller than it, and then recursively sort. But quick sort is not a stable sorting method, its time complexity is not deterministic, its average sorting time complexity is O(nlogn), but in special cases, its worst time complexity is O(n ^2) is the same as bubble sort, and its basic sorting rules are as follows:

So its implementation uses recursive calls, and its implementation code is as follows:
int a[101]
void Quicks_Sort(int begin,int end){//Bring in the start position and end position
	if(begin>end)//If the start is greater than the end
		return;
	int tmp=a[begin];//Determine the base
	int i=begin;
	int j=end;
	while(i!=j){
		while(a[j]>=tmp&&j>i)//Find a number smaller than the base
			j--;
		while(a[i]<=tmp&&j>i)//Find a number larger than the base
			i++;
		if(j>i){//Exchange both
			int t=a[i];
			a[i]=a[j];
			a[j]=t;
		}
	}
	a[begin]=a[i];//Swap the base and its position
	a[i]=tmp;
	Qiicks_Sort(begin,i-1);//Iteration on the left
	Qicks_Sort(i+1,end);//Iteration on the right
}

Among them, it is necessary to arrange the right side first and then the left side. Otherwise, in some cases, when the left side and the right side reach the same position first, there will be problems in sorting. For specific examples, please refer to the following blog: http://blog. csdn.net/w282529350/article/details/50982650
In the example, the latter j needs to be smaller than i, so the sorting is wrong. Finally, it should be mentioned that most of the above programs are implemented by c, and the real STL source code should be implemented by iterators or iterator operator overloading. , not STL source code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728803&siteId=291194637