常用排序算法(冒泡/选择/快速)

下面介绍几种面试经常遇到的排序方法 常用大全排序

1.冒泡排序

public void bubbleSort(int[] score){
        System.out.println("通过冒泡排序方法对数组进行排序:");
        for (int i = 0; i < score.length - 1; i++) {
            // 比较相邻两个元素,较大的数往后冒泡
            for (int j = 0; j < score.length - 1 - i; j++) {
                if (score[j] > score[j + 1]) {
                    int temp = score[j + 1]; // 把第一个元素值保存到临时变量中
                    score[j + 1] = score[j]; // 把第二个元素值转移到第一个元素变量中
                    score[j] = temp; // 把临时变量(第一个元素的原值)保存到第二个元素中
                }
                System.out.print(score[j] + " "); // 对排序后的数组元素进行输出
            }
            System.out.print("【");
            for (int j = score.length - 1 - i; j < score.length; j++) {
                System.out.print(score[j] + " ");
            }
            System.out.println("】");
        }
    }

2.选择排除

public static void main(String[] args) {
        int[] number = {13,15,24,99,4,1};
        String end = "\n";
        int index;
        for (int i = 1;i < number.length;i++) {
            index = 0;
            for(int j = 1;j <= number.length-i;j++) {
                if (number[j] > number[index]) {
                    index = j;    // 查找最大值
                }
            }
            end = number[index] + " " + end;    // 定位已排好的数组元素
            int temp = number[number.length-i];
            number[number.length-1] = number[index];
            number[index] = temp;
            System.out.print("【");
            for (int j = 0;j < number.length-i;j++) {
                System.out.print(number[j]+" ");
            }
            System.out.print("】"+end);
        }
    }

3.快速排序(三种写法)


 public static void main(String[] args) {
        int[] number={13,15,24,99,14,11,1,2,3};
        System.out.println("排序前:");
        for(int val:number) {
            System.out.print(val+" ");
        }
         quickSort(number,0,number.length-1);
         System.out.println("\n排序后:");
        for(int val:number) {
            System.out.print(val +" ");
        }
  }

    /**
     * 入口函数(递归方法),算法的调用从这里开始。
     */
    public static  void quickSort(int[] arr, int startIndex, int endIndex) {
        if (startIndex >= endIndex) {
            return;
        }

        // 核心算法部分:分别介绍 双边指针(交换法),双边指针(挖坑法),单边指针
        //int pivotIndex = doublePointerSwap(arr, startIndex, endIndex);
        // int pivotIndex = doublePointerHole(arr, startIndex, endIndex);
         int pivotIndex = singlePointer(arr, startIndex, endIndex);

        // 用分界值下标区分出左右区间,进行递归调用
        quickSort(arr, startIndex, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, endIndex);
    }
  /**
     * 双边指针(交换法)
     * 思路:
     * 记录分界值 pivot,创建左右指针(记录下标)。
     * (分界值选择方式有:首元素,随机选取,三数取中法)
     *
     * 首先从右向左找出比pivot小的数据,
     * 然后从左向右找出比pivot大的数据,
     * 左右指针数据交换,进入下次循环。
     *
     * 结束循环后将当前指针数据与分界值互换,
     * 返回当前指针下标(即分界值下标)
     */
    private static int doublePointerSwap(int[] arr, int startIndex, int endIndex) {
        int pivot = arr[startIndex];
        int leftPoint = startIndex;
        int rightPoint = endIndex;

        while (leftPoint < rightPoint) {
            // 从右向左找出比pivot小的数据
            while (leftPoint < rightPoint
                    && arr[rightPoint] > pivot) {
                rightPoint--;
            }
            // 从左向右找出比pivot大的数据
            while (leftPoint < rightPoint
                    && arr[leftPoint] <= pivot) {
                leftPoint++;
            }
            // 没有过界则交换
            if (leftPoint < rightPoint) {
                int temp = arr[leftPoint];
                arr[leftPoint] = arr[rightPoint];
                arr[rightPoint] = temp;
            }
        }
        // 最终将分界值与当前指针数据交换
        arr[startIndex] = arr[rightPoint];
        arr[rightPoint] = pivot;
        // 返回分界值所在下标
        return rightPoint;
    }

    /**
     * 双边指针(挖坑法)
     * 思路:
     * 创建左右指针。
     * 记录左指针数据为分界值 pivot,
     * 此时左指针视为"坑",里面的数据可以被覆盖。
     *
     * 首先从右向左找出比pivot小的数据,
     * 找到后立即放入左边坑中,当前位置变为新的"坑",
     * 然后从左向右找出比pivot大的数据,
     * 找到后立即放入右边坑中,当前位置变为新的"坑",
     *
     * 结束循环后将最开始存储的分界值放入当前的"坑"中,
     * 返回当前"坑"下标(即分界值下标)
     */
    private static  int doublePointerHole(int[] arr, int startIndex, int endIndex) {
        int pivot = arr[startIndex];
        int leftPoint = startIndex;
        int rightPoint = endIndex;

        while (leftPoint < rightPoint) {
            // 从右向左找出比pivot小的数据,
            while (leftPoint < rightPoint
                    && arr[rightPoint] > pivot) {
                rightPoint--;
            }
            // 找到后立即放入左边坑中,当前位置变为新的"坑"
            if (leftPoint < rightPoint) {
                arr[leftPoint] = arr[rightPoint];
                leftPoint++;
            }
            // 从左向右找出比pivot大的数据
            while (leftPoint < rightPoint
                    && arr[leftPoint] <= pivot) {
                leftPoint++;
            }
            // 找到后立即放入右边坑中,当前位置变为新的"坑"
            if (leftPoint < rightPoint) {
                arr[rightPoint] = arr[leftPoint];
                rightPoint--;
            }
        }
        // 将最开始存储的分界值放入当前的"坑"中
        arr[rightPoint] = pivot;
        return rightPoint;
    }

    /**
     * 单边指针
     * 思路:
     * 记录首元素为分界值 pivot, 创建标记 mark 指针。
     * 循环遍历与分界值对比。
     * 比分界值小,则 mark++ 后与之互换。
     * 结束循环后,将首元素分界值与当前mark互换。
     * 返回 mark 下标为分界值下标。
     */
    private static  int singlePointer(int[] arr, int startIndex, int endIndex) {
        int pivot = arr[startIndex];
        int markPoint = startIndex;

        for (int i = startIndex + 1; i <= endIndex; i++) {
            // 如果比分界值小,则 mark++ 后互换。
            if (arr[i] < pivot) {
                markPoint++;
                int temp = arr[markPoint];
                arr[markPoint] = arr[i];
                arr[i] = temp;
            }
        }
        // 将首元素分界值与当前mark互换
        arr[startIndex] = arr[markPoint];
        arr[markPoint] = pivot;
        return markPoint;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值