QuickSortDemo

package com.suning.sntcscase.controller.MutiThread;

import static jdk.nashorn.internal.objects.Global.print;

public class QuickSortDemo {

    public static void main(String[] args) {
        int[] arr = {5, 6, 7, 81, 2, 66, 88, 17, 99};
         printArr(arr);
        quickSort(arr, 0, arr.length - 1);
        System.out.println();
        printArr(arr);
    }

    private static void printArr(int[] arr) {
        for(int i=0;i<arr.length; i++){
            System.out.print(arr[i] +"\t");
        }
    }

    private static void quickSort(int[] arr, int low, int high) {
        int start = low;
        int end = high;
        int key = arr[low];

        //从右往左找小
        while (end > start) {

            while (end > start && arr[end] >= key)
                end--;

            if (arr[end] <= key) {
                int tmp = arr[end];
                arr[end] = arr[start];
                arr[start] = tmp;
            }


            //从左往右大
            while (end > start && arr[start] <= key)
                start++;


            if (arr[start] >= key) {
                int tmp = arr[start];
                arr[start] = arr[end];
                arr[end] = tmp;
            }

        }

        ///递归
     if(start <low)   quickSort(arr,0,start-1);
     if(end <high)   quickSort(arr,end+1,high);




    }






}

  

posted @ 2019-10-16 14:19  Alamps  阅读(214)  评论(0编辑  收藏  举报