首页 > 其他 > 详细

QuickSortDemo

时间:2019-10-16 14:59:17      阅读:46      评论:0      收藏:0      [点我收藏+]
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);




    }






}

  

QuickSortDemo

原文:https://www.cnblogs.com/alamps/p/11685287.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!