Quick sort principle and C++ source code implementation


1. Principle

Split the data to be sorted into two independent parts by sorting, and all the data in one part is smaller than all the data in the other part, and then according to this method to quickly sort the two parts of the data, the whole sorting process It can be done recursively so that the entire data becomes an ordered sequence.


2. Thinking

2.1 Basic ideas

  1. First set a boundary value, and divide the array into left and right parts by the boundary value.

  2. Collect the data smaller than the cutoff value to the left of the array, and collect the data greater than or equal to the cutoff value to the right of the array.

  3. The left and right arrays can be sorted independently: the data on the left selects a demarcation value, and the data on the right selects a demarcation value. According to the operation of step 2, complete the grouping between the size of the demarcation value in each group.

  4. Repeat the above grouping steps to complete the sorting.

2.2 Illustration of the idea:
Insert picture description here
Find the value smaller than the cutoff value of 23 from the right to the left of j, exchange the value with 23, and record the position j.

Insert picture description here
From the left of i to the right, look for data larger than the cutoff value of 23, exchange values ​​with 23, and record position i.

Insert picture description here
From the right to the left of j, look for values ​​that are smaller than the cutoff value of 23, exchange values ​​with 23, and record position j.

Insert picture description here

From the left of i to the right, look for data larger than the cutoff value of 23, exchange values ​​with 23, and record position i.
Insert picture description here
From the right of j to the left, look for values ​​that are smaller than the cutoff value of 23, exchange values ​​with 23, and record position j.

2.3 Sorting example :
Assume that the initial sequence {xi} is: 5, 3, 7, 6, 4, 1, 0, 2, 9, 10, 8.

  1. At this time, ref=5, i=1, j=11, looking from back to front, the first number smaller than 5 is x8=2, so the sequence is: 2, 3, 7, 6, 4, 1, 0,5,9,10,8.

  2. At this time i=1, j=8, looking from the front to the back, the first number greater than 5 is x3=7, so the sequence is: 2, 3, 5, 6, 4, 1, 0, 7, 9, 10, 8.

  3. At this time, i=3, j=8, looking forward from the 8th place, the first number smaller than 5 is x7=0, so: 2, 3, 0, 6, 4, 1, 5, 7, 9, 10, 8.

  4. At this time, i=3, j=7, looking backward from the third place, the first number greater than 5 is x4=6, so: 2, 3, 0, 5, 4, 1, 6, 7, 9, 10, 8.

  5. At this time, i=4, j=7, looking forward from the 7th place, the first number smaller than 5 is x6=1, so: 2, 3, 0, 1, 4, 5, 6, 7, 9, 10, 8.

  6. At this time, i=4, j=6, look backwards from the 4th place, until the 6th place there is a number greater than 5, at this time, i=j=6, ref becomes a dividing line, the number before it Both are smaller than it, and the subsequent numbers are larger than it. The same method can be used to sort the two parts before and after.


Three, quick sort features

  • The time-consuming is mainly reflected in the use of demarcation value grouping, to divide an array of length m, a total of m-1 demarcation value comparisons are required.

  • The worst time complexity of ordinary quick sort is O(n2).

  • The expected time complexity is O(nlogn).

  • No auxiliary array space is required.


Four, C++ source code implementation

void sort(int *data, int left, int right)
{
    
    
	if (left >= right) 
		return ;

	int i = left;
	int j = right;
	int key = data[left];

	while (i < j) 
	{
    
    
		//后面的数据一直大于哨兵key,这个时候一直往前比对 (前<- ->后)
		while (i < j && key <= data[j])
 		{
    
      
			j --;
		}
		/*退出以上循环说明,后面往前的数值小于哨兵,这个时候把后面的值替换到前面的位置*/
		data[i] = data[j];   

		//哨兵一直大于后面的值,这个时候一直往后比对
		while (i < j && key >= data[i]) 
		{
    
      
			i ++;
		}
		/*退出以上循环说明,哨兵往后的数值大于哨兵,这个时候把后面的值替换到前面的位置*/
		data[j] = data[i]; 
	}

	data[i] = key;

	sort(data, left, i - 1);
	sort(data, i + 1, right);
}

void quick_sort(int *data, int length) 
{
    
    
		sort(data, 0, length-1);
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/110182677
Recommended