[Detailed] Java implements quick sort

1 Overview

    How is the quick sort sorted? Why is it called quick sort? high speed?

    Quick sort uses the idea of ​​divide and conquer, and divides the sequence to be sorted into two parts through a quick sort. One part records elements smaller than the keyword, and the other part records elements larger than the keyword, so that the entire sequence is ordered. the goal of.

Specific thoughts:

① Take out an element in the sequence to be sorted as a benchmark, called a benchmark record

② Define two indexes left and right, which respectively represent the first index and the last index, and the key is the reference value

③ First, the tail index is scanned forward to find a record smaller than the reference value, and replace the value corresponding to the first index

④ Then, the tail index is scanned backwards until a record larger than the reference value is found, and bin replaces the value corresponding to the tail index

⑤ In the scanning process, the first index is equal to the last index (left = right), then the sorting ends; replace the reference value (key) with the value corresponding to the first index

⑥When  performing the next sorting, the sequence to be sorted is divided into two areas: [0,left-1],[righ+1,end]

⑦ Repeat the above steps until the sorting is completed

Graphical presentation:

2. Code demonstration

/*
 * 快速排序:找一个基准元素,
 * 两个索引:尾索引指向的序列的最后一个元素,首索引指向的是第一个元素
 * 尾索引向前扫描,直到找到比基准元素小的元素,放到首索引的位置
 * 首索引向后扫描,直到找到比基准元素大的元素,放到尾索引的位置
 * 在扫描过程中,当left=right(首索引和尾索引相遇的时候),则一趟排序结束,将基准值key替换首索引所对应的位置
 * 进行下一趟排序,待排成的序列分为:[0,left-1],[right + 1, end]
 * 重复以上操作
 */
// 快速排序是 从往两边排序 中间的序列是有序的
public class QuickSort {
	
	public static void main(String[] args) {
		int[] arr = {21,12,31,1,3,53,9};
		quickSort(arr, 0, 6);
		for(int i = 0;i < arr.length;i++) {
			System.out.print(arr[i] + "  ");
		}
	}
	
	private static void quickSort(int[] array,int leftIndex,int rightIndex) {
		// 判断是否首索引比尾索引大
		if(leftIndex >= rightIndex) {
			return;                                                                                               
		}
		//设置基准元素和首尾索引
		int left = leftIndex;
		int right = rightIndex;
		int key = array[left];
		
		// 当符合条件的时候
		while(left < right) {
			// 从右往左  尾索引向左扫描  找比基准元素小的
			while(left < right && array[right] >= key) {
				// 符合条件的话就往前走
				right--;
			}
			// 当条件结束的时候 把符合条件的元素 找比基准元素大的
			array[left] = array[right];
			
			while(left < right && array[left] <= key) {
				left++;
			}
			
			// 找到这种元素然后放到后面
			array[right] = array[left];
			
			// 当left == right的时候,基准位置赋值
			array[left] = key;
			
		}
			// 对基准位左边的位置递归排序
			quickSort(array,leftIndex,left-1);
			// 对基准位右边的位置递归排序
			quickSort(array,right + 1,rightIndex);
	}
}

3. Analysis of algorithm complexity

       Quick sort is faster, because compared to bubble sort, each exchange is skipped. Set a reference point every time you sort, put all the numbers less than or equal to the reference point to the left of the reference point, and put all the numbers greater than or equal to the reference point to the right of the reference point. In this way, each time it is exchanged, it will not be like bubble sorting and can only exchange between adjacent numbers each time, and the exchange distance will be much larger. Therefore, the total number of comparisons and exchanges is reduced, and the speed is naturally increased. Of course, in the worst case, two adjacent numbers may still be exchanged. Therefore, the worst time complexity of quick sort and bubble sort is the same as O(N2), and its average time complexity is O(NlogN).

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/114877265