数据结构与算法java-快速排序

quicksort1是传统的取首数字,但是此时仅适用于数组完全随机,如不随机,复杂度为O(n2)
一定要先走最后的指针,从后往前走

quicksort2是另一种取法,取中位数,此方法也省去了最后交换首数字的一部,简单了一点点
注意:i,j在处理和tmp相同的数字时,要进行相同的操作,保持数组两端平衡,否则会skrewed,影响效率

排序的稳定性判断条件:输出的数组是否和输入数组有关联
j接下来是自己想的方法:(我发现我的有问题dbq

public static int[] countingRadixSort3(int[] arr, int num) {
        final int BUCKETS = 10;
        int[] out = new int[arr.length];
        int[] tmp = new int[arr.length];
        int[] count = new int[BUCKETS];
        for(int m =0,n =1;m<num;m++,n*=10) {
            for (int i = 0; i < arr.length; i++) {
                count[arr[i]/n % 10]++;
            }
            for (int i = 1; i < BUCKETS; i++) {
                count[i] += count[i - 1];
            }

            for (int i = arr.length - 1; i >= 0; i--) {
                out[--count[arr[i]/n%10]] = arr[i];
            }
            //arr = out; this is not right cuz arr points to out, insead of copying out
            for(int i=0;i<out.length;i++){
                arr[i] =out[i];
            }
            count = new int[BUCKETS];
        }
        return arr;
    }

不过大佬的方法在最下面

package Course2.ch07;

public class quickSort {
    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);
    }

    public static void quickSort2(int[] arr,int low,int high){
        int i,j,temp,t;
        if(low>high){
            return;
        }
        i=low;
        j=high;
        //temp就是基准位
        temp = arr[(low+high)/2];

        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);
    }

    public static void main(String[] args){
        int[] arr = {10,7,2,4,7,62,3,4,2,1,8,9,19};
        quickSort(arr, 0, arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        int[] arr2 = {10,7,2,4,7,62,3,4,2,1,8,9,19};
        quickSort2(arr2, 0, arr2.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr2[i]+" ");
        }
    }
}
public static <AnyType extends Comparable<? super AnyType>>
    void insertionSort(AnyType[] arr) {
        int len = arr.length;

        int i;
        int j;

        for (i = 1; i < len; i++) {
            AnyType temp = arr[i];
            for (j = i; j > 0 && temp.compareTo(arr[j - 1]) < 0; j--) {
                arr[j] = arr[j - 1];
            }
            arr[j] = temp;
        }
    }

    public static <AnyType extends Comparable<? super AnyType>>
    void quickSort(AnyType[] arr) {
        quickSort(arr, 0, arr.length - 1);
    }

    private static <AnyType extends Comparable<? super AnyType>>
    void quickSort(AnyType[] a, int left, int right) {
        if (left == right) {
            return;
        }
        if (left + CUTOFF <= right) {
            AnyType pivot = median3(a, left, right);

            // Begin partitioning
            int i = left, j = right - 1;
            for (; ; ) {
                while (a[++i].compareTo(pivot) < 0) {
                }
                while (a[--j].compareTo(pivot) > 0) {
                }
                if (i < j)
                    swapReferences(a, i, j);
                else
                    break;
            }

            swapReferences(a, i, right - 1);   // Restore pivot

            quickSort(a, left, i - 1);    // Sort small elements
            quickSort(a, i + 1, right);
        } else {
            insertionSort(a);
        }
    }
    ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值