排序算法及其效率比较

排序算法及其效率比较

下面比较了冒泡排序法选择排序法快速排序法的效率,代码如下:

package org.iword;

import java.util.Arrays;

public class Main {
    public static void main(String[] args)
    {
        int[] bubble = getIntArray(20, 50);
        int[] select = Arrays.copyOf(bubble, bubble.length);
        int[] quick = Arrays.copyOf(bubble, bubble.length);

        System.out.println("--------------------冒泡排序法--------------------");
        System.out.println("排序前:");
        forEach(bubble);
        System.out.println("\n排序后:");
        long bubbleStart = System.nanoTime();
        Sort.bubbleSort(bubble);
        long bubbleEnd = System.nanoTime();
        forEach(bubble);
        System.out.println("\n排序耗时:" + (bubbleEnd - bubbleStart));
        System.out.println(check(bubble));

        System.out.println("\n--------------------选择排序法--------------------");
        System.out.println("排序前:");
        forEach(select);
        System.out.println("\n排序后:");
        long selectStart = System.nanoTime();
        Sort.selectSort(select);
        long selectEnd = System.nanoTime();
        forEach(select);
        System.out.println("\n排序耗时:" + (selectEnd - selectStart));
        System.out.println(check(select));

        System.out.println("\n--------------------快速排序法--------------------");
        System.out.println("排序前:");
        forEach(quick);
        System.out.println("\n排序后:");
        long quickStart = System.nanoTime();
        Sort.quickSort(quick);
        long quickEnd = System.nanoTime();
        forEach(quick);
        System.out.println("\n排序耗时:" + (quickEnd - quickStart));
        System.out.println(check(quick));
    }

    // 输出数组
    private static void forEach(int[] numbers)
    {
        for (int number : numbers) {
            System.out.printf("%d ", number);
        }
    }

    // 获取长度为 length 且元素值小于 max 的整型数组
    private static int[] getIntArray(int length, int max)
    {
        int[] numbers = new int[length];
        for (int i = 0; i < length; i++) {
            numbers[i] = (int) (max * Math.random());
        }

        return numbers;
    }

    // 检测 numbers 中是否有未排序的元素
    private static String check(int[] numbers)
    {
        if (numbers.length < 2) {
            return "未检测";
        }

        for (int i = 0; i < numbers.length - 1; i++) {
            if (numbers[i] > numbers[i + 1]) {
                return String.format("无序元素索引:%d, %d", i, (i + 1));
            }
        }

        return "已排好序";
    }
}

// 以下为三种排序法实现代码
package org.iword;

public class Sort {
    // 选择排序法
    public static void selectSort(int[] numbers)
    {
        int min;
        for (int i = 0; i < numbers.length - 1; i++) {
            min = i;
            for (int j = i + 1; j < numbers.length; j++) {
                if (numbers[j] < numbers[min]) {
                    min = j;
                }
            }
            swap(numbers, i, min);
        }
    }

    // 快速排序法
    public static void quickSort(int[] numbers)
    {
        quickSort(numbers, 0, numbers.length - 1);
    }

    // 快速排序法
    private static void quickSort(int[] numbers, int left, int right)
    {
        if (left < right) {
            int i = left;
            int j = right;
            int mark = numbers[left];
            while (i < j) {
                // 从右向左查找
                while (numbers[j] >= mark && j > i) {
                    j--;
                }
                if (j != i) {
                    numbers[i] = numbers[j];
                    i++;
                }
                // 从左向右查找
                while (numbers[i] < mark && i < j) {
                    i++;
                }
                if (i != j) {
                    numbers[j] = numbers[i];
                    j--;
                }
            }
            numbers[i] = mark;
            quickSort(numbers, left, i - 1);
            quickSort(numbers, i + 1, right);
        }
    }

    // 冒泡排序法
    public static void bubbleSort(int[] numbers)
    {
        boolean isLoop = true;
        for (int i = numbers.length - 1; isLoop && i > 0; i--) {
            isLoop = false; // 假定数组已排好序,此时不需要继续循环
            for (int j = 0; j < i; j++) {
                // 检测无序元素
                if (numbers[j] > numbers[j + 1]) {
                    swap(numbers, j, j + 1); // 交换值
                    isLoop = true; // 继续循环
                }
            }
        }
    }

    // 交换数组 arr 下标为 i 和 j 两个元素的值
    private static void swap(int[] arr, int i, int j)
    {
        if (arr[i] != arr[j]) {
            arr[i] += arr[j];
            arr[j] = arr[i] - arr[j];
            arr[i] -= arr[j];
        }
    }
}

效率最高的是快速排序法,尤其是当数组中元素较多的时候,选择排序法效率其次,冒泡排序法效率最低,若数组中的大部分元素已排好序,此时冒泡排序法效率反而会比较高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值