Java programming: sorting algorithm-quick sort

Introduction to Quick Sort

Quicksort (Quicksort) is an improvement to bubble sort. The basic idea is: divide the data to be sorted into two independent parts by sorting, all the data in one part is smaller than all the data in the other part, and then quickly sort the two parts of the data according to this method. The entire sorting process can be carried out recursively, so that the entire data becomes an ordered sequence

Schematic diagram of quick sort method

Insert picture description here
Insert picture description here

Application examples of quick sorting

Requirement: To sort [-9,78,0,23,-567,70] from small to large, the quick sort method is required. [Test 8w and 800w]
Description [Verification Analysis]:

  1. If you cancel the left and right recursion, the result is -9 -567 0 23 78 70
  2. If you cancel right recursion, the result is -567 -9 0 23 78 70
  3. If you cancel the left recursion, the result is -9 -567 0 23 70 78

Code

package sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class QuickSort {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    8,1,3,6,4,2,5};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
        // 测速
//        int[] arr = new int[80000];
//        for (int i = 0; i < 80000; i++) {
    
    
//            arr[i] = (int) (Math.random() * 80000);// 生成一个0-80000的数据
//        }
//        Date date1 = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String date1Str = simpleDateFormat.format(date1);
//        System.out.println("排序前的时间为:" + date1Str);
//        quickSort(arr,0,arr.length-1);
//        Date date2 = new Date();
//        String date2Str = simpleDateFormat.format(date2);
//        System.out.println("排序后的时间为:" + date2Str);
    }

    public static void quickSort(int[] arr, int left, int right) {
    
    
        int l = left;   // 左下标
        int r = right;  // 右下标
        // pivot 是中轴值
        int pivot = arr[(left + right) / 2];
        int temp = 0;   // 临时变量
        // while循环的目的是让比pivot值小的值放到其左边,大的放到右边
        while (l < r) {
    
    
            // 在pivot左边一直找,找到一个大于等于pivot的值
            while (arr[l] < pivot) {
    
    
                l += 1;
            }
            // 在pivot右边一直找,找到一个小于等于pivot的值
            while (arr[r] > pivot) {
    
    
                r -= 1;
            }
            // 如果l>=r 说明pivot的左右两边的值,已经左边小于pivot右边大于pivot的值
            if (l >= r) {
    
    
                break;
            }
            // 交换
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;

            // 如果交换完成后发现arr[l] == pivot 的值相等,那么让r--,前移一步
            if (arr[l] == pivot) {
    
    
                r -= 1;
            }
            // 如果交换完成后发现arr[r] == pivot 的值相等,那么让l--,后移一步
            if (arr[r] == pivot) {
    
    
                l += 1;
            }
        }

        // 如果l==r,必须l++ r--,否则栈溢出
        if (l == r) {
    
    
            l += 1;
            r -= 1;
        }
        if (left < r) {
    
    
            quickSort(arr, left, r);
        }
        if (right > l) {
    
    
            quickSort(arr, l, right);
        }
    }
}

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108864287