数组中三种排序算法(快速,冒泡,选择)

快速排序的思想:

排序思想: 从数列中挑出一个元素,称为"基准"(pivot), 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区结束之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。 递归的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了。虽然一直递归下去,但是这个算法总会结束,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。

快速排序的原理图:

代码示例:

 @Test
    public void quickSortTest() {
        int[] a = {2, 19, 10, 3, 5, 8, 29, 10, 88};

        quickSort(a);
        System.out.println(Arrays.toString(a));
    }

    public void quickSort(int arr[]) {
        subSort(arr, 0, arr.length - 1);
    }

    private void subSort(int[] arr, int head, int tail) {
        if (head < tail) {
            int base = arr[head];
            int startIndex = head;
            int endIndex = tail + 1;
            //1. 遍历数组,无限循环,直到前指针大于后指针
            while (true) {
                //2. 前指针后移,后指正前移。找出前大后小的元素后,调换位置
                while (startIndex < tail && arr[++startIndex] <= base) ;
                while (endIndex > head && arr[--endIndex] >= base) ;
                if (startIndex < endIndex) {
                    //3. 调换位置,保证左边的元素小于右边的元素
                    swap(arr, startIndex, endIndex);
                } else {
                    break;  //4. 结束本回合
                }
            }
            //5. 基点与后指针对应元素调换位置
            swap(arr, head, endIndex);
            //这时后指针的前面和后面的元素,相对于原有的基点都是左小,右大。
            //6. 再以 后指针分成左右两组,分别执行上面的操作
            subSort(arr, head, endIndex - 1);
            subSort(arr, endIndex + 1, tail);

        }//7. 迭代到每组只有一个元素(head==tail),结束!
    }

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

冒泡排序代码示例:

//1,冒泡排序
    public void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

选择排序代码示例:

/**
     * 2, 选择排序
     *
     * 将第一个元素与其余的元素比较,先把最小元素的数组索引保存在一个临时变量中。
     * 将最小的元素与当前的元素位置调换。
     */
     /**
     * 选择排序
     *
     * @param arr
     */
    public void selectSort(Comparable[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;     //最小值对应的索引
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minIndex].compareTo(arr[j]) > 0) {
                    minIndex = j;
                }
            }
            //最小值的不是自己本身,调换位置
            if (minIndex != i) {
                Comparable temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }

        }

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值