美文网首页
2019-12-23 排序

2019-12-23 排序

作者: cpp_75f6 | 来源:发表于2019-12-23 13:24 被阅读0次

快速排序

[1, 3, 9, 7, 6]
[1, 3, 9, 7, 6]
[1, 3, 9, 7, 6]
[1, 3, 6, 7, 9]
[1, 3, 6, 7, 9]
"https://blog.csdn.net/shujuelin/article/details/82423852

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) {
               swap(arr,i,j);
            }

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

反转排序

    public void revertSort(){
        int[] a = {1, 3, 9, 7, 6, 4};
        for (int i = 0; i < a.length/2; i++) {
            if (i != a.length-1-i) {
                int temp = a[i];
                a[i] = a[a.length-1-i];
                a[a.length-1-i] = temp;
            }
            System.out.println(Arrays.toString(a));

        }
    }

直接选择排序

public  void directSelectSort() {
        int[] a = {1, 3, 9, 7, 6};
        int maxTempIndex;
        for (int j = 0; j < a.length; j++) {
            maxTempIndex = 0;
            for (int i = 0; i < a.length-j; i++) {
                // 循环取出值最大的数值和索引
                if (a[maxTempIndex] < a[i]) {
                    maxTempIndex = i;
                }
            }
            // 将最大值放入a[a.length-j-1]
            int tempInt = a[maxTempIndex];
            a[maxTempIndex] = a[a.length-j-1];
            a[a.length-j-1] = tempInt;
            System.out.println(Arrays.toString(a));

        }
    }

冒泡排序

public void bubbleSort() {
        // 1、1 3 9 7 6
        // 2、1 3 6 7 9
        int[] a = {1, 3, 9, 7, 6};
        int count = 0;
        for (int j = 0; j < a.length - 1; j++) {
            for (int i = 0; i < a.length - j - 1; i++) {
                int temp = a[i];
                if (a[i] > a[i + 1]) {
                    a[i] = a[i + 1];
                    a[i + 1] = temp;
                }
                System.out.println(Arrays.toString(a));
                count++;
            }
        }
        System.out.println(count);
    }

交换函数

  public  static void  swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

main函数

    public static void main(String[] args) {
        JsonArrayTest j = new JsonArrayTest();
        int[] a = {1, 3, 9, 7, 6};
        j.quickSort(a, 0, a.length-1);;
        j.bubbleSort();
        j.directSelectSort();
        j.revertSort();
    }

相关文章

网友评论

      本文标题:2019-12-23 排序

      本文链接:https://www.haomeiwen.com/subject/rzyznctx.html