快速排序

package sort;

/**
 * Copyright (c) 2020 bigdata ALL Rights Reserved
 * Project: learning
 * Package: sort
 * Version: 1.0
 *
 * @author qingzhi.wu 2020/8/20 9:34
 */
public class QuicksortTest {
    public static void main(String[] args) {
        int[] arr = {1,234,45,666,4,5,7,9,3,2};

        quicksort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+",");
        }

    }

    public static void quicksort(int[] arr){
        assert arr != null;
        quicksort(arr,0,arr.length-1);
    }

    private  static void quicksort(int[] arr, int left, int right) {

        if (left >= right) {
            return;
        }

        int base = arr[left];
        int i = left;
        int j = right;

        while (i<j){

            while (arr[j] >= base && i < j  ){
                j--;
            }

            while (arr[i] <= base && i < j){
                i++;
            }

            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        arr[left] = arr[i];
        arr[i]  = base;

        quicksort(arr,left,i-1);
        quicksort(arr,i+1,right);
    }
}

你可能感兴趣的:(数据结构与算法)