模拟冒泡排序、插入排序、希尔排序、三路快速排序对n条数据的处理时间。

       使用一维数组存储n个随机数组成的无序数组进行模拟测试

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Random;

public class Main {

    //冒泡排序
    public static void sortMP (Comparable []array){
        Integer size = array.length;
        boolean isEnd = false;
        while (!isEnd) {
            isEnd = true;//标记数组是否还要继续排序
            for (int j = 0; j + 1 < size; j++) {
                if (array[j + 1].compareTo(array[j]) < 0) {
                    swap(array, j, j + 1);
                    isEnd = false;//有排序行为
                }
            }
        }
    };

    //插入排序
    public static void  sortInsert (Comparable []array ){
        Integer size = array.length;
        for (int i = 0;i < size;i++){
            for (int j = i;j > 0;j--){
                if (array[j].compareTo(array[j-1]) < 0) {
                    swap(array,j-1,j);
                }
            }
        }
    }

    //希尔排序
    public static  void sortXE (Comparable []array){
        Integer size = array.length;
        for (int gap = size/2;gap > 0;gap /= 2){
            for (int i = gap;i < size;i++){
                for (int j =i;j >= gap && array[j].compareTo(array[j-gap]) < 0;j -= gap){
                    swap(array,j-gap,j);
                }
            }
        }
    }

    //快速排序
    
//快速排序
// 递归使用快速排序,对arr[l...r]的范围进行排序
private static void partition(Comparable[] arr, int l, int r){
    if (l >= r) {
        return;
    }
    // 随机在arr[l...r]的范围中, 选择一个数值作为标定点pivot
    swap( arr, l, (int)(Math.random()*(r-l+1)) + l );
    Comparable v = arr[l];
    int lt = l;     // arr[l+1...lt] < v
    int gt = r + 1; // arr[gt...r] > v
    int i = l+1;    // arr[lt+1...i) == v
    while( i < gt ){
        if( arr[i].compareTo(v) < 0 ){
            swap( arr, i, lt+1);
            i ++;
            lt ++;
        }
        else if( arr[i].compareTo(v) > 0 ){
            swap( arr, i, gt-1);
            gt --;
        }
        else{ // arr[i] == v
            i ++;
        }
    }
    swap( arr, l, lt );
    partition(arr, l, lt-1);
    partition(arr, gt, r);
}


public static void sortQK(Comparable[] arr){
    Integer size = arr.length;
    partition(arr, 0, size-1);
}

    //交换left和right位置上的元素
    public static void swap(Object [] array,int left, int right){
        Object temp = array[right];
        array[right] = array[left];
        array[left] = temp;
    }

    //获取随机数组成的无序数组
    public static Integer[] randomArray(int size, int bound){
        Integer []x = new Integer[size];
        Random random = new Random();
        for (int i = 0;i < size;i++){
            x[i] = random.nextInt(bound);
        }
        return x;
    }

    public static void main(String[] args) {
        int size = 100000;//数组长度
        int bound = 255555;//数组元素范围
        Integer []a = randomArray(size,bound);
        Integer []b = randomArray(size,bound);
        Integer []c = randomArray(size,bound);
        Integer []d = randomArray(size,bound);

        System.out.println("处理"+size+"条数据...");

        Thread ta = new Thread(new Runnable() {
            @Override
            public void run() {
//                System.out.println("数组a:");
//                for(Integer i : a) {
//                    System.out.print(i +" ");
//                }
                LocalTime past = LocalTime.now();

                Main.sortInsert(a);

                LocalDateTime now = LocalDateTime.now();

//                System.out.println("\n排序后的数组:");
//
//                for(Integer i : a) {
//                    System.out.print(i +" ");
//                }
                System.out.println("插入排序所用时间:"+Duration.between(past,now).toMillis()+"ms");
            }
        },"ThreadA");

        Thread tb = new Thread(new Runnable() {
            @Override
            public void run() {
//                System.out.println("数组b:");
//                for(Integer i : b) {
//                    System.out.print(i +" ");
//                }
                LocalDateTime past = LocalDateTime.now();

                Main.sortXE(b);

                LocalDateTime now = LocalDateTime.now();
//                System.out.println("\n排序后的数组:");
//
//                for(Integer i : b) {
//                    System.out.print(i +" ");
//                }
                System.out.println("希尔排序所用时间:"+Duration.between(past,now).toMillis()+"ms");
            }
        },"ThreadB");

        Thread tc = new Thread(new Runnable() {
            @Override
            public void run() {
                LocalDateTime past = LocalDateTime.now();

                Main.sortMP(c);

                LocalDateTime now = LocalDateTime.now();

                System.out.println("冒泡排序所用时间:"+Duration.between(past,now).toMillis()+"ms");
            }
        },"ThreadC");

        Thread td = new Thread(new Runnable() {
            @Override
            public void run() {
                LocalDateTime past = LocalDateTime.now();

                Main.sortQK(d);

                LocalDateTime now = LocalDateTime.now();

                System.out.println("三路快速排序所用时间:"+Duration.between(past,now).toMillis()+"ms");
            }
        },"ThreadD");

        ta.start();
        tb.start();
        tc.start();
        td.start();
    }
}

本次模拟测试处理100000条数据

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值