快速排序比stream.sorted()快

本文探讨了Java中使用快速排序算法与Stream API进行排序的性能差异。通过示例代码展示了如何实现快速排序,并对比了两种排序方式的耗时。实验结果显示,尽管Stream API使用简便,但在特定场景下,传统算法如快速排序可能具有更高的效率。
package test;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @Author: xinjingjie
 * @Date:2021/5/28 9:10
 **/
public class ListDemo {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> numbers = new ArrayList<>();
        new Random().ints().limit(2000).forEach(numbers ::add);
        Integer[] integers1=numbers.toArray(new Integer[0]);
        Integer[] integers2=numbers.toArray(new Integer[0]);

        //可见返回的是不同的
        System.out.println(integers1==integers2);

        System.out.println(Arrays.toString(integers1));
        System.out.println(numbers);


        System.out.println();
        System.out.println();
        System.out.println();

        System.out.println("排序前"+Arrays.toString(integers1));
        List<Integer> newInts = new ArrayList<>();
        int[]  beforeInt = Arrays.stream(integers2).mapToInt(Integer::valueOf).toArray();
        Instant s1 =Instant.now();
//        Arrays.stream(integers1).sorted().filter(value -> value != 0).forEach(newInts ::add);
        int[]  tempInt =Arrays.stream(beforeInt).sorted().toArray();;
        Instant e1 =Instant.now();
        System.out.println("耗时"+Duration.between(s1,e1).toMillis()+"ms");
        System.out.println("排序后"+Arrays.toString(tempInt));


        System.out.println();
        System.out.println();
        System.out.println();

        int[]  tempInts = Arrays.stream(integers2).mapToInt(Integer::valueOf).toArray();
        System.out.println("排序前"+Arrays.toString(tempInts));
        Instant s2 =Instant.now();
//        int[]  tempInts = Arrays.stream(integers2).filter(integer -> integer != 0).mapToInt(Integer::valueOf).toArray();
        quickSort(tempInts,0,integers2.length-1);
        Instant e2 =Instant.now();
        System.out.println("耗时"+Duration.between(s2,e2).toMillis()+"ms");
        System.out.println("排序后"+Arrays.toString(tempInts));

        System.out.println();
        System.out.println();
        System.out.println();


        System.out.println("排序后"+numbers);
        Instant s3 =Instant.now();
//        List<Integer> collect = numbers.stream().sorted().filter(integer -> integer != 0).collect(Collectors.toList());
        List<Integer> collect = numbers.stream().sorted().collect(Collectors.toList());
        Instant e3 =Instant.now();
        System.out.println("耗时"+Duration.between(s3,e3).toMillis()+"ms");
        System.out.println("排序后"+collect);





    }
    /*
    快速排序
     */
    public static void quickSort(int[] arr,int low,int high){
        int i,j,temp,t;
        if(low>high){
            return;
        }
        i=low;
        j=high;
        //temp就是基准位
        temp = arr[low];
        while (i<j) {
            //先看右边,依次往左递减
            while (temp<=arr[j]&&i<j) {
                j--;
            }
            //再看左边,依次往右递增
            while (temp>=arr[i]&&i<j) {
                i++;
            }
            //如果满足条件则交换
            if (i<j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }

        }
        //最后将基准为与i和j相等位置的数字交换
        arr[low] = arr[i];
        arr[i] = temp;
        //递归调用左半数组
        quickSort(arr, low, j-1);
        //递归调用右半数组
        quickSort(arr, j+1, high);
    }

}

结果

false
耗时5ms

耗时1ms

耗时11ms

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值