时间排序算法(结合多线程)面试

分享最近看到的一道面试题

  • 面试题内容是给你一个数组,使用多线程进行排序输出,如何设计?

  • 大概设计思路就是每拿到一个值就睡眠这个值的秒数然后再输出

  • 附上代码:

public class TimeWarm {

    public static int pos = 0;

    public static Object object = new Object();

    public static int[] arr = new int[9];

    static {
        for(int i=0;i<arr.length;i++) {
            arr[i] = 10 - i;
        }
    }
    public static void main(String[] args) {
        for(int i=0;i< arr.length;i++){
            new Thread(new Runner()).start();
        }
    }

    public static class Runner implements Runnable{

        public int waitTime = 0;
        @Override
        public void run(){
            synchronized (object){
                waitTime = arr[pos];
                pos++;
            }
            try {
                TimeUnit.SECONDS.sleep(waitTime);
                System.out.println(waitTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

附上常用的排序以及代码:

  • 冒泡排序
 public class BubbleSort {

    //冒泡排序
    public static void bubbleSort(int[] arr, boolean ascending) { //exchange标志表示为升序排序还是降序排序

        boolean flag = true; 

        for (int i = 1; i < arr.length && flag; i++) 

            flag = false; //假定未交换

            for (int j = 0; j < arr.length - i; j++) {

                if (ascending ? arr[j] > arr[j + 1] : arr[j] < arr[j + 1]) { 
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }

            }
        }
    }

    //冒泡排序 -- 默认不传参升序
    public static void bubbleSort(int[] arr) {
        bubbleSort(arr, true);
    }
}
  • 快速排序
public class QuickSort {

    //快速排序
    public static void quickSort(int[] arr) {
        quickSort(arr, true);
    }

    public static void quickSort(int[] arr, boolean ascending) {
        if (ascending) {
            quickSort(arr, 0, arr.length - 1, true);
        } else {
            quickSort(arr, 0, arr.length - 1, false);
        }
    }

    public static void quickSort(int[] arr, int begin, int end, boolean ascending) {
        if (ascending)
            quickSort(arr, begin, end);
        else
            quickSortDescending(arr, begin, end);
    }

    public static void quickSort(int[] arr, int begin, int end) {
        if (begin > end) { //结束条件
            return;
        }
        int base = arr[begin];
        int i = begin, j = end;
        while (i < j) { 
            while (arr[j] >= base && i < j) { 
                j--;
            }
            while (arr[i] <= base && i < j) { 
                i++;
            }
            if (i < j) { //如果满足条件则交换
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }

        }
    
        arr[begin] = arr[i];
        arr[i] = base;
        quickSort(arr, begin, i - 1); 
        quickSort(arr, i + 1, end); 

    }

    //快排序降序
    public static void quickSortDescending(int[] arr, int begin, int end) {
        if (begin > end) { 
            return;
        }
        int base = arr[begin];
        int i = begin, j = end;
        while (i < j) { 
            while (arr[j] <= base && i < j) { 
                j--;
            }
            while (arr[i] >= base && i < j) { 
                i++;
            }
            if (i < j) { 
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }

        }
        //最后将基准为与i和j相等位置的数字交换
        arr[begin] = arr[i];
        arr[i] = base;
        quickSortDescending(arr, begin, i - 1);
        quickSortDescending(arr, i + 1, end); 

    }

}
  • 直接选择排序
public class SelectSort {

    //直接选择排序
    public static void selectSort(int[] arr, boolean ascending) {
        for (int i = 0; i < arr.length; i++) {
            int m = i; 
            for (int j = i + 1; j < arr.length; j++) {
                if (ascending ? arr[j] < arr[m] : arr[j] > arr[m]) {
                    m = j; 
                }

            }
            //交换位置
            int temp = arr[i];
            arr[i] = arr[m];
            arr[m] = temp;

        }
    }

    public static void selectSort(int[] arr) {
        selectSort(arr, true);
    }
}
  • 堆排序
public class HeapSort {

    //堆排序
    public static void heapSort(int[] arr) {
        heapSort(arr, true);
    }

    public static void heapSort(int[] arr, boolean maxheap) {


        for (int i = arr.length / 2 - 1; i >= 0; i--) {
            sift(arr, i, arr.length , maxheap);
        }


        for (int j = arr.length - 1; j > 0; j--) {
            int temp = arr[j];
            arr[j] = arr[0];
            arr[0] = temp;


            sift(arr, 0, j , maxheap);
        }
    }


    private static void sift(int[] arr, int parent, int len, boolean maxheap) {

        int value = arr[parent]; //先取出当前元素i

        for (int child = 2 * parent + 1; child < len; child = child * 2 + 1)

            if (child+1 < len && (maxheap ? arr[child] < arr[child + 1] : arr[child] > arr[child + 1])) { 
                child++; 
            }


            if (maxheap ? value < arr[child] : value > arr[child]) {
                arr[parent]=arr[child];
                parent = child;
            }
            else {
                break;
            }
        }
        arr[parent] =value; 


    }

}
  • 归并排序
public class MergeSort {

    //归并排序
    public static void mergeSort(int []arr ,boolean ascending){
        int[] temp = new int[arr.length]; 
        mergeSort(arr,0,arr.length-1,temp,ascending);
    }
    public static void mergeSort(int []arr){
        mergeSort(arr,true);
    }


    public static void mergeSort(int []arr,int left,int right,int[] temp,boolean ascending){
        if(left<right){ 

            int mid = left + (right-left)/2;


            mergeSort(arr,left,mid,temp,ascending); 
            mergeSort(arr,mid+1,right,temp,ascending); 

            merge(arr,left,mid,right,temp,ascending); 
        }
    }

    private static void merge(int[] arr,int left,int mid,int right,int[] temp,boolean ascending){
        int i = left; 
        int j = mid+1;
        int t = 0; //临时数组指针
        while(i<=mid&&j<=right){
            if(ascending?arr[i]<arr[j]:arr[i]>arr[j]){ //比较两个序列第一个元素谁小,谁小先拷贝谁到temp,然后对应子序列下标加1
                temp[t++] = arr[i++];
            }else {
                temp[t++] = arr[j++];
            }
        }

        while(i<=mid){ //将左边剩余元素填充进temp中——左序列有一些数总是比右边的大的数
            temp[t++] = arr[i++];
        }

        while(j<=right){ //将右序列剩余元素填充进temp中——右序列有一些数总是比左边的大的数
            temp[t++] = arr[j++];
        }

        t = 0;

        //将temp中的元素全部拷贝到原数组中
        while(left<=right){
            arr[left++] = temp[t++];
        }

    }

}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yunson.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值