Quick sort algorithm and principle

1. Quick sort principle

The basic idea of ​​this method is:

  • 1. First take a number from the number sequence as the reference number.
  • 2. In the partitioning process, all numbers larger than this number are placed on the right side, and all numbers less than or equal to it are placed on the left side.
  • 3. Repeat the second step for the left and right intervals until there is only one number in each interval.

最本质的总结:
Quick sort, to put it bluntly, is the process of finding the correct index position for the benchmark data.

As shown in the figure below, assuming that the initial benchmark data is the first element 23 of the array, first use a temporary variable to store the benchmark data, that is, tmp=23; then scan the array from both ends of the array and set two indicators :low points to the starting position, high points to the end.

  
   Insert picture description here

First of all, starting from the second half, if the scanned value is greater than the reference data, reduce high by 1. If there is an element that is smaller than the value of the reference data (18<=tmp in the above figure), assign the value of the high position to Low position, the results are as follows:
   Insert picture description here

Then start scanning from front to back. If the scanned value is less than the reference data, add 1 to low. If an element is found to be larger than the value of the reference data (as shown in Figure 46=>tmp), then assign the value of the low position to the high position. The result after the pointer moves and data exchange is as follows:
Insert picture description here

Then start scanning from back to front. The principle is the same as above, and it is found that the above figure 11<=tmp, then the value of the high position is assigned to the value of the low position, and the results are as follows:
Insert picture description here

Then start to traverse from front to back until low=high ends the loop, at this time the subscript of low or high is 基准数据23在该数组中的正确索引位置as shown in the figure below.
Insert picture description here

After walking down this way, you can clearly know that in fact 快速排序的本质就是把基准数大的都放在基准数的右边,把比基准数小的放在基准数的左边,这样就找到了该数据在数组中的正确位置.
  , the first half and the second half are sorted separately in the recursive manner in the future. When the current half and the second half are in order, the array will be naturally ordered.

2. Code ideas

You can see from the above process:

  • ① Scan forward from the end of the line and when low <high, if a[high]> tmp, then high--, but if a[high] <tmp, then assign the value of high to low, that is, arr[low ] = a[high], at the same time, the array scanning method needs to be changed, that is, it needs to scan from the beginning of the team to the end of the team
  • ② Similarly, when scanning from the head of the team to the end of the team, if a[low] <tmp, then low++, but if a[low]> tmp, you need to assign the value of the low position to the high position. That is, arr[low] = arr[high], and at the same time change the array scanning mode to scanning from the end of the line to the head of the line.
  • ③ Keep repeating ① and ②, knowing that low>=high (actually low=high), the position of low or high is the correct index position of the reference data in the array...

3. Find the index of the benchmark number

Here is another picture to deepen the impression of finding the index;
Insert picture description here

4. Reference code

import java.util.Arrays;

public class QuickSort {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = {
    
     49, 38, 65, 97, 23, 22, 76, 1, 5, 8, 2, 0, -1, 22 };
		quickSort(arr, 0, arr.length - 1);
		System.out.println("排序后:");
		for (int i : arr) {
    
    
			System.out.print(i + "  ");
		}
	}

	private static void quickSort(int[] arr, int low, int high) {
    
    

		if (low < high) {
    
    
			// 找寻基准数据的正确索引
			int index = getIndex(arr, low, high);
			System.out.println("排序后" + Arrays.toString(arr));
			// 进行迭代对index之前和之后的数组进行相同的操作使整个数组变成有序
			//quickSort(arr, 0, index - 1); 之前的版本,这种姿势有很大的性能问题,谢谢大家的建议
			quickSort(arr, low, index - 1);
			quickSort(arr, index + 1, high);
		}

	}

	private static int getIndex(int[] arr, int low, int high) {
    
    
		// 基准数据
		int tmp = arr[low];
		while (low < high) {
    
    
			// 当队尾的元素大于等于基准数据时,向前挪动high指针
			while (low < high && arr[high] >= tmp) {
    
    
				high--;
			}
			// 如果队尾元素小于tmp了,需要将其赋值给low
			arr[low] = arr[high];
			// 当队首元素小于等于tmp时,向前挪动low指针
			while (low < high && arr[low] <= tmp) {
    
    
				low++;
			}
			// 当队首元素大于tmp时,需要将其赋值给high
			arr[high] = arr[low];
		}
		// 跳出循环时low和high相等,此时的low或high就是tmp的正确索引位置
		// 由原理部分可以很清楚的知道low位置的值并不是tmp,所以需要将tmp赋值给arr[low]
		arr[low] = tmp;
		return low; // 返回tmp的正确位置
	}
}

The operating effects are as follows:
Insert picture description here


Reference article:
https://blog.csdn.net/nrsc272420199/article/details/82587933
https://github.com/doubleview/data-structure


Finish

Guess you like

Origin blog.csdn.net/baidu_21349635/article/details/114693527