【Quick Sort】|C++ code implementation|From simple to complex, easy to get started

First, the question of quick sort?

When we think of sorting, and we only think about the basic grammar + the most violent method, it is to use the method of loop traversal to find the most suitable insertion position. What we need to pay attention to is that this is an algorithm with O(n 3 ) complexity , so that the sorting data range (how much data) will be relatively small, at this time we have to find a way to optimize this algorithm, so that this sorting algorithm can be implemented in a wider range of data.

At the same time, we thought of bucket sorting , but bucket sorting is greatly affected by the data range (data size), and we cannot complete this sorting operation when the data value is relatively large (once the data range reaches 10 7 or more on possible TLE)

Of course, we have another way, the simple bubble sorting idea, first store all the values ​​​​in the array, we use the related ideas of reverse order pairs in linear algebra

Set the pointer to loop through twice, if the latter value is larger than the previous one, we will perform an exchange operation

Although the time complexity of bubble sorting has been reduced to O(n 2 ), the time complexity is still not enough for our huge data range... At this time, we wonder if there is an algorithm that can perfectly adapt to various sorting operations. Thus, quick sort appeared.AIt

Second, the algorithm idea of ​​​​quick sorting

Quick sort is essentially a recursive idea (composed of some simple loops). When we encounter an array of n numbers that needs to be sorted, the first thing we think of is to save the array. After saving, we How should I do it?

Maybe we can set a middle value and divide the left and right sides into two arrays ( array A and array B ). All elements in array A are smaller than the middle value mid , and all elements in array B are greater than the middle value mid . In this case, The data on the left and right sides of mid has been completely processed, and the data on the left must be in the left group of data no matter how it is transformed.
insert image description here

After completing this step, we have processed part of the sorting operation of this large array, which can be regarded as the difference between these two arrays ( we will merge Mid into one of the front and rear arrays in the code implementation, so there are only two arrays ) There is an ordering relationship between them. Then we perform the sorting operation on the small arrays separated below. After several recursive operations, each of the two arrays we separated has only one element.

We know that there is an order relationship between the left and right arrays, so we can directly determine that this order is the final order when n (array element) == 1, and we can just jump out of the loop after the assignment is completed.
AIt

Third, the code implementation of quick sort

First of all, the first problem that quick sorting should deal with is to divide the contents of the array into two arrays and divide them by mid. First , the selection of mid must be inside the array. The mid we choose is recommended to use the value generated by random numbers. , but for the convenience of understanding, we choose the middle value of the array as the mid value each time. Next, let's complete the size comparison operation

#include <iostream>
using namespace std ;

void quick_sort(int l , int r , int q[])
//q[]是要进行排序的数组 ,l 是他最小的下标 , r是他最大的下标 
{
    
    
	int i = l - 1 , j = r + 1 , x = q[l + r >> 1] ;
	// 我们下面要使用到 do while 循环 ,我们把指针都扩大1 ,x取q的中值,l + r >> 1 为位运算 ,相当于(l + r) / 2 
	while ( i < j )
	{
    
    
		do i ++ ; while(a[i] < x );
		do j -- ; while(a[j] > x );
		//寻找前面比x大的和后面比x小的进行交换
		if(i < j )
		{
    
    
			swap(a[i],a[j]);
		}    
}

After the completion, we have to think about the following recursive operation, what should we do, according to the previous search, we know that the mid array is stored in the B array, we have to think about where to divide the remaining two arrays Most suitable, first we can simulate this operation. (The above line is the initial array, agree with Isshiki)

insert image description hereThis is one of the special examples, where i == j, in general, i = j + 1; of course, this situation is generated because the intermediate array is available on both sides. This article only reminds readers that i != j + 1 in this case, but it can still be regarded as i = j + 1 during operation to avoid entering the black hole of thinking. This part can be skipped directly. After thinking about this problem, we can simply It is found that the subscripts [ l , j ] and [ j +1 , r ] are divided into two arrays.

quick_sort(l , j , q[]);
quick_sort(j + 1 , r , q[]);

After completing these two operations, let's look at the entire algorithm of quick sort

#include <iostream>
using namespace std ;

const int N = 100010 ;

void quick_sort (int l ,int r ,int q[])
{
    
    
	if(l >= r )	return ;
	int i = l - 1 , j = r + 1 ,x = q[(r + l )>> 1];
	while (i < j ) 
	{
    
    
		do i ++ ; while (q[i] < x );
		do j -- ; while (q[j] > x );
		if(i < j )
			swap(q[i],q[j]);
	}
	quick_sort(l , j , q);
	quick_sort(j + 1 , r , q) ;
}
int main ()
{
    
    
	int n ;
	cin >> n;
	int q[N] ;
	for(int i = 0 ; i < n ; i ++ )
		cin >> q[i] ;
	quick_sort(0,n - 1,q);
	for(int i = 0 ; i < n ; i ++ )
		cout << q[i] << " " ;
		return 0 ;
}

4. Algorithm thinking and analysis of quick sort

1. Analysis of Algorithm Complexity

The highest time complexity of the quick sort algorithm is O(n 2 ) , and the lowest time complexity is O(nlog 2 n)

The most complicated time occurs when traversing to the minimum or maximum value is used as the intermediate value of the algorithm every time, and our second traversal will be performed n times

The simplest time complexity occurs around each perfect separation, and the traversed graph is like a complete binary tree;

2, Thinking about the middle number of mid

Optimal solution
The entire number can be randomly selected using the random() random array under the ctime header file to prevent data freaks from hank
universality
The entire intermediate number can take the value in any array

Guess you like

Origin blog.csdn.net/wen030803/article/details/131644828