Quick sort of sorting algorithm (Java description)

Quick sort
Insert picture description here

Recursively to its ancestral grave


public class QuickSort {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = {
    
    3,4,6,7,2,7,2,8,0,9,1};
		quickSort(arr, 0, arr.length-1);
		System.out.println(Arrays.toString(arr));
	}
	
	public static void quickSort(int[] arr, int start, int end){
    
    
		if(start < end){
    
    
			//把开始位置当做标准数
			int stard = arr[start];
			//记住需要排序的下标
			int low = start;
			int high= end;
			//循环找比标准数大的数和比标准数小的数
			while(low < high){
    
    
				//不用替换(右边的数字比标准数大)
				while(low < high && stard <= arr[high]){
    
    
					high--;
				}
				//使用右边的数字替换左边的数(比较后比标准数小的替换)
				arr[low] = arr[high];
				
				while(low < high && arr[low] <= stard){
    
    
					low++;
				}
				arr[high] = arr[low];
				
			}
			arr[low] = stard;
			quickSort(arr, 0, low);
			quickSort(arr, low+1, end);
		}
	}
}

Time complexity
Average O(nlog2n)
Best O(nlog2n)
Worst O(n2)
Space complexity
O(nlog2n)

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108817453