左程云 Java 笔记--排序


插入排序

//插入排序
    public static void insertionsort(int[] arr){
        if (arr == null || arr.length < 2){
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = i-1; j >= 0 ; j--) {
                if (arr[j] > arr[j+1]){
                    sway(arr,j,j+1);
                }
            }
        }
    }

归并排序

    //归并排序
    public static void process(int[] arr,int L, int R){
        if (L == R){
            return;
        }
        int mid = L + ((R - L) >> 1);
        process(arr,L,mid);
        process(arr,mid+1,R);
        merge(arr,L,mid,R);
    }

    private static void merge(int[] arr, int l, int mid, int r) {
        int[] help = new int[r-l+1];
        int i = 0;
        int p1 = l;
        int p2 = mid+1;
        while (p1 <= mid && p2 <= r){
            help[i++] = arr[p1] <= arr[p2]? arr[p1++] : arr[p2++];
        }
        while (p1 <= mid){
            help[i++] = arr[p1++];
        }
        while (p2 <= r){
            help[i++] = arr[p2++];
        }
        for (int j = 0; j < help.length; j++) {
            arr[l+j] = help[j];
        }
    }

求小合

public static int process(int[] arr,int left,int right){
        if (left == right){
            return 0;
        }
        int mid = left + ((right - left) >> 1);
        return process(arr,left,mid)
                + process(arr,mid+1,right)
                + merge(arr,left,mid,right);
    }

    private static int merge(int[] arr, int left, int mid, int right) {
        int[] help = new int[right-left+1];
        int i = 0;
        int p1 = left;
        int p2 = mid+1;
        int res = 0;
        while (p1 <= mid && p2 <= right){
            res += arr[p1] < arr[p2] ? (right - p2 + 1) * arr[p1] : 0;
            help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
        }
        while (p1 <= mid){
            help[i++] = arr[p1++];
        }
        while (p2 <= right){
            help[i++] = arr[p2++];
        }
        for (int j = 0; j < help.length; j++) {
            arr[left+j] = help[j];
        }
        return res;
    }

快排3.0

    public static void quickSort(int[] arr,int left,int right){
        if (left < right){
            swap(arr,left+(int)(Math.random()*(right-left+1)),right);
            int[] p = partition(arr,left,right);
            quickSort(arr,left,p[0]-1);
            quickSort(arr,p[1]+1,right );
        }
    }

    private static int[] partition(int[] arr, int left, int right) {
        int less = left -1;
        int more = right;
        while (left < more){
            if (arr[left] < arr[right]){
                swap(arr, ++less, left++);
            }else if (arr[left] > arr[right]){
                swap(arr,--more,left);
            }else {
                left++;
            }
        }
        swap(arr,more,right);
        return new int[] {less+1,more};
    }

    public static void swap(int[] arr,int a,int b){
        int temp=0;
        temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

在这里插入图片描述

    //插入一个数
    public static void heapinsert(int[] arr, int index){
        while (arr[index] > arr[(index-1)/2]){
            swap(arr,index,(index-1)/2);
            index = (index-1)/2;
        }
    }

    //移除一个数
    public static void heapify(int[] arr, int index, int heapsize){
        int left = index*2+1;
        while (left < heapsize){
            int largest = left+1 < heapsize && arr[left+1]>arr[left]
                    ? left+1 : left;
            largest = arr[largest] > arr[index] ? largest:index;
            if (largest == index){
                break;
            }
            swap(arr,largest,index);
            index = largest;
            left = index*2+1;
        }
    }

    public static void swap(int[] arr,int a,int b){
        int temp=0;
        temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

堆排序

    //堆排序
    public static void heapsort(int[] arr){
        if (arr==null || arr.length<2){
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            heapinsert(arr,i);
        }
        int heapsize = arr.length;
        swap(arr,0,--heapsize);
        while (heapsize > 0){
            heapify(arr,0,heapsize);
            swap(arr,0,--heapsize);
        }
    }

小根堆

在这里插入图片描述
小根堆就是优先级队列
在这里插入图片描述
扩容代价低

    //小根堆
    public static void sortArrDistanceLessK(int[] arr, int k){
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        int index = 0;
        for (; index <= Math.min(arr.length, k); index++) {
            heap.add(arr[index]);
        }
        int i = 0;
        for (; index < arr.length; index++,i++) {
            heap.add(arr[index]);
            arr[i] = heap.poll();
        }
        while (!heap.isEmpty()){
            arr[i++] = heap.poll();
        }
    }

比较器

在这里插入图片描述

    //比较器
    public static class AComp implements Comparator<Integer> {
        //如果返回负数,第一个数在上面
        //如果返回正数,第二个数在上面
        //如果返回0,无所谓
        // (假设o1<o2,升序o1-o2,降序o2-o1)
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    }

用法:
	PriorityQueue<Integer> haed = new PriorityQueue<Integer>(new AComp());

桶排序

在这里插入图片描述
在这里插入图片描述

   public static void radixsort(int[] arr){
        if (arr == null || arr.length < 2){
            return;
        }
        radixSort(arr,0,arr.length-1,maxbits(arr));
    }

    public static int maxbits(int[] arr){
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max,arr[i]);
        }
        int res = 0;
        while (max != 0){
            res++;
            max /= 10;
        }
        return res;
    }

    public static void radixSort(int[] arr, int left, int right, int digit){
        final int radix = 10;
        int  j =0;
        int[] bucket = new int[right-left+1];
        for (int k = 1; k <= digit; k++) {
            int[] count = new int[radix];
            for (int l = left; l <= right ; l++) {
                j = getDigit(arr[l],k);
                count[j]++;
            }
            for (int l = 1; l < radix ; l++) {
                count[l] = count[l]+count[l-1];
            }
            for (int l = right; l >= left  ; l--) {
                j = getDigit(arr[l],k);
                bucket[count[j]-1] = arr[l];
                count[j]--;
            }
            for (int i = left,o=0; i < right; i++,o++) {
                arr[i] = bucket[o];
            }
        }
    }

    private static int getDigit(int i, int k) {
        return ((i/((int) Math.pow(10,k-1)))%10);
    }
}

总结

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值