选择排序、冒泡排序、插入排序(Java代码)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

选择排序、冒泡排序、插入排序(Java代码)


工具类:

import java.util.Arrays;

/**
 *
 * @title: ArrayUtils
 * @Date: 2020.11.26 15:10
 */
public class ArrayUtils {

    /**
     * @Description 随机生成数组
     * @Date 2020.11.26 15:14
     * @Version  1.0
     */
    public static int[] generatorRandomArray(int maxSize, int maxValue, boolean defaultSize) {
        // Math.random()   [0,1)
        // Math.random() * N  [0,N)
        // (int)(Math.random() * N)  [0, N-1]
        int[] arr = new int[defaultSize ? maxSize : (int) ((maxSize + 1) * Math.random())];
        for(int i=0;i<arr.length;i++) {
            arr[i] =(int) (maxValue * (Math.random() + 1));
        }
        System.out.print("随机生成的int数组:");
        SystemUtils.printArray(arr);
        return arr;
    }

    /**
     * @Description 打印int数组
     * @Date 2020.11.26 15:14
     * @Version  1.0
     */
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    /**
     * @Description 复制数组
     * @Date 2020.11.26 15:36
     * @Version  1.0
     */
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    /**
     * @Description 校验两个数组是否一样
     * @Date 2020.11.26 15:36
     * @Version  1.0
     */
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * @Description 数组排序
     * @Date 2020.11.26 15:37
     * @Version  1.0
     */
    public static void sort(int[] arr) {
        Arrays.sort(arr);
    }

    /**
     * @Description int数组选择排序
     * @Date 2020.11.26 16:48
     * @Version  1.0
     */
    public static void intArraySelectionSort(int[] arr) {
        if(arr == null || arr.length < 2) {
            return;
        }
        for(int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for(int j = i + 1; j < arr.length; j++) {
                minIndex = arr[j] < arr[minIndex] ? j : minIndex;
            }
            exchangeValue(arr, i, minIndex);
        }
    }

    /**
     * @Description 冒泡排序
     * @Date 2020.11.26 17:04
     * @Version  1.0
     */
    public static void intArrayBubbleSort(int[] arr) {
        if(arr == null || arr.length < 2) {
            return;
        }
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
//                    arr[i] = arr[i] ^ arr[j];
//                    arr[j] = arr[i] ^ arr[j];
//                    arr[i] = arr[i] ^ arr[j];
                    exchangeValue(arr, j, j + 1);
                }
            }
        }
    }

    /**
     * @Description 交换值
     * @Date 2020.11.26 17:06
     * @Version  1.0
     */
    public static void exchangeValue(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

1、选择排序

/**
 *选择排序
 * @title: LeetCode_002_SelectSort
 * @Date: 2020.11.26 14:08
 */
public class LeetCode_002_SelectSort {

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 10;
        int maxValue = 10;
        boolean succeed = true;
        boolean defaultSize = false;
        for(int i = 0; i < testTime; i++) {
            int[] arr = ArrayUtils.generatorRandomArray(maxSize, maxValue, defaultSize);
            int[] arr1 = ArrayUtils.copyArray(arr);

            ArrayUtils.sort(arr);
            System.out.print("Arrays.sort(arr)排序后的数组arr:");
            SystemUtils.printArray(arr);

            selectionSort(arr1);
            System.out.print("选择排序后的数组arr1:");
            SystemUtils.printArray(arr1);
            if (!ArrayUtils.isEqual(arr, arr1)) {
                succeed = false;
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");
    }

    public static void selectionSort(int[] arr) {
        if(arr == null || arr.length < 2) {
            return;
        }
        for(int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for(int j = i + 1; j < arr.length; j++) {
                minIndex = arr[j] < arr[minIndex] ? j : minIndex;
            }
            ArrayUtils.exchangeValue(arr, i, minIndex);
        }
    }
}

冒泡排序

/**
 *  冒泡排序
 * @title: LeetCode_003_BubbleSort
 * @Date: 2020.11.26 16:53
 */
public class LeetCode_003_BubbleSort {

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 10;
        int maxValue = 10;
        boolean succeed = true;
        boolean defaultSize = false;
        for(int i = 0; i < testTime; i++) {
            int[] arr = ArrayUtils.generatorRandomArray(maxSize, maxValue, defaultSize);
            int[] arr1 = ArrayUtils.copyArray(arr);

            ArrayUtils.sort(arr);
            System.out.print("Arrays.sort(arr)排序后的数组arr:");
            SystemUtils.printArray(arr);

            bubbleSort(arr1);
            System.out.print("冒泡排序后的数组arr1:");
            SystemUtils.printArray(arr1);
            if (!ArrayUtils.isEqual(arr, arr1)) {
                succeed = false;
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");
    }

    public static void bubbleSort(int[] arr) {
        if(arr == null || arr.length < 2) {
            return;
        }
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    ArrayUtils.exchangeValue(arr, j, j + 1);
                }
            }
        }
    }
}

插入排序

/**
 * Description: TODO
 *
 * @title: LeetCode_004_InsertSort
 */
public class LeetCode_004_InsertSort {

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 10;
        int maxValue = 10;
        boolean defaultSize = false;
        boolean seccess = true;
        for(int i = 0; i < testTime; i++) {
            int[] arr = ArrayUtils.generatorRandomArray(maxSize,maxValue,defaultSize);
            int[] arr1 = ArrayUtils.copyArray(arr);

            intArrayInsertSort(arr);
            System.out.print("插入排序后的数组arr:");
            ArrayUtils.printArray(arr);

            ArrayUtils.sort(arr1);
            System.out.print("sort排序后的数组arr1:");
            ArrayUtils.printArray(arr1);
            if(!ArrayUtils.isEqual(arr, arr1)) {
                seccess = false;
                break;
            }
        }
        System.out.println(seccess ? "NICE!" : "FUCKING FUCKED!");
    }

    /**
     * @Description
     * @Date 2020.11.26 21:32
     **/
    public static void intArrayInsertSort(int[] arr) {
        if(arr == null || arr.length < 2) {
            return;
        }
        for(int i = 1; i < arr.length; i++) {
            for(int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
                ArrayUtils.exchangeValue(arr, j, j+1);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软软的铲屎官

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值