快速排序(多线程)实现方式

记录快速排序和多线程快速排序性能耗时
10次排序取均值
在这里插入图片描述

public class QuickSortUtils {

    public static void main(String[] args) throws Exception {
        int total = 4000000;
        int[] list = new int[total];
        for (int i = 0; i < total; i++) {
            int rand = (int) (Math.random() * 400000100);
            list[i] = rand;
        }
//        printList(list);//打印排序前数组
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        //多次试验得出50万数据以上多线程快速排序耗时较少
//        quickSort(list, 0, list.length);//快速排序实现
        threadQuickSort(list, 0, list.length, 3);//多线程快速排序实现
        stopWatch.stop();
//        printList(list);//打印排序后数组
        System.out.println("耗时:" + (new BigDecimal(stopWatch.getNanoTime()).divide(new BigDecimal(1000 * 1000), 2, RoundingMode.DOWN)) + "ms");

    }

    /**
     * @param depth 深度
     */
    private static void qucikSortSpaceSplit(List<int[]> depthSort, int[] list, int start, int end, int depth, int threadNum) {
        if (depth == threadNum) {
            int[] ints = new int[2];
            ints[0] = start;
            ints[1] = end;
            depthSort.add(ints);
        }
        if (depth > threadNum) {
            return;
        }
        depth++;
        int i = getQuickSortIndex(list, start, end);
        if (i == -1) {
            return;
        }
        qucikSortSpaceSplit(depthSort, list, start, i, depth, threadNum);
        qucikSortSpaceSplit(depthSort, list, i, end, depth, threadNum);
        return;
    }

    /**
     * @param threadNum 线程数:threadNum² 值建议不要大于10
     */
    public static void threadQuickSort(int[] list, int start, int end, int threadNum) throws InterruptedException {
        List<int[]> spaceSplit = new ArrayList<>();
        qucikSortSpaceSplit(spaceSplit, list, start, end, 0, threadNum);
        CountDownLatch countDownLatch = new CountDownLatch(spaceSplit.size());
        for (int[] ints : spaceSplit) {
            int instart = ints[0];
            int inend = ints[1];
            new Thread(new Runnable() {
                @Override
                public void run() {
                    quickSort(list, instart, inend);
                    countDownLatch.countDown();
                }
            }).start();
        }
        countDownLatch.await();
    }

    private static int getQuickSortIndex(int[] list, int start, int end) {
        if (end - start <= 2) {
            if (end - start == 2) {
                if (list[start] > list[end - 1]) {
                    int temp = list[start];
                    list[start] = list[end - 1];
                    list[end - 1] = temp;
                }
            }
            return -1;
        }
        int base = list[start];//11
        int i = start;
        int j = end;
        a:
        for (int k = start + 1; k < end; k++) {
            if (i >= j) {
                break a;
            }
            int rval = list[--j];//7
            while (rval > base) {
                if (i == j) {
                    break a;
                }
                if (i + 1 == j) {
                    break;
                }
                rval = list[--j];
            }
            if (i == j) {
                list[start] = rval;//4
                list[i] = base;//44
                break a;
            }
            int lval = list[++i];
            while (lval < base) {
                if (i == j) {
                    list[start] = lval;
                    list[j] = base;
                    break a;
                }
                lval = list[++i];
            }
            list[i] = rval;
            list[j] = lval;
        }
        return i;

    }

    public static void quickSort(int[] list, int start, int end) {
        int i = getQuickSortIndex(list, start, end);
        if (i == -1) {
            return;
        }
        quickSort(list, start, i);
        quickSort(list, i, end);
    }

    private static void printList(int[] list) {
        for (int i1 : list) {
            System.out.print(i1 + ",");
        }
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值