冒泡排序 选择排序 插入排序

插入排序

package com.nobody.sort;

/**
 * @author Mr.nobody
 * @Description 插入排序
 * @date 2020/9/5
 */
public class Code01_InsertionSort {

    public static void insertionSort(int[] arr) {
        // 数组为空,或者数组长度小于2就没必要操作
        if (null == arr || arr.length < 2) {
            return;
        }
        // 依次让0~0,0~1,0~2,...,0~n-1区间的数有序
        // 则需要每次让1,2,3,...,n-1位置上的数与左边的所有数据进行比较
        // 每次比较,如果小于左边的数就进行交换,直到不小于左边的数
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
                swap(arr, j, j + 1);
            }
        }
    }

    // 采用异或操作交换两个数
    private static void swap(int[] arr, int i, int j) {
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }
}

选择排序

package com.nobody.sort;

/**
 * @author Mr.nobody
 * @Description 选择排序
 * @date 2020/9/5
 */
public class Code02_SelectionSort {

    public static void selectionSort(int[] arr) {
        // 数组为空,或者数组长度小于2就没必要操作
        if (null == arr || arr.length < 2) {
            return;
        }
        // 每次从位置为0,1,2,...,n-1开始,与后面的数比较,
        // 找出第i小的数的位置,将其数与i位置的数交换
        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;
            }
            if (i != minIndex) {
                swap(arr, i, minIndex);
            }
        }
    }

    // 采用异或操作交换两个数
    private static void swap(int[] arr, int i, int j) {
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }
}

冒泡排序

package com.nobody.sort;

/**
 * @author Mr.nobody
 * @Description 冒泡排序
 * @date 2020/9/5
 */
public class Code03_BubbleSort {

    public static void bubbleSort(int[] arr) {
        // 数组为空,或者数组长度小于2就没必要操作
        if (null == arr || arr.length < 2) {
            return;
        }
        // 每次从左边第一个元素开始,依次和后面的数据比较,大于后面数据就交换,
        // 每次与后面的比较的次数为n-1,n-2,...1次,每次都把大数放在最右边
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }

    // 采用异或操作交换两个数
    private static void swap(int[] arr, int i, int j) {
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }
}

测试

package com.nobody.sort;

import java.util.Arrays;

/**
 * @author Mr.nobody
 * @Description
 * @date 2020/9/5
 */
public class Main {

    public static void main(String[] args) {
        int[] arr = {1, 5, 3, 2, 8, 10, 11, 3, 4, 0};
        System.out.print("原始数组:");
        Arrays.stream(arr).forEach(e -> System.out.print(e + " "));
        System.out.println();
        
        Code01_InsertionSort.insertionSort(arr);
        System.out.print("插入排序:");
        Arrays.stream(arr).forEach(e -> System.out.print(e + " "));
        System.out.println();

        int[] arr1 = {1, 5, 3, 2, 8, 10, 11, 3, 4, 0};
        Code02_SelectionSort.selectionSort(arr1);
        System.out.print("选择排序:");
        Arrays.stream(arr1).forEach(e -> System.out.print(e + " "));
        System.out.println();

        int[] arr2 = {1, 5, 3, 2, 8, 10, 11, 3, 4, 0};
        Code03_BubbleSort.bubbleSort(arr2);
        System.out.print("冒泡排序:");
        Arrays.stream(arr2).forEach(e -> System.out.print(e + " "));
        System.out.println();
    }
}

测试结果

原始数组:1 5 3 2 8 10 11 3 4 0 
插入排序:0 1 2 3 3 4 5 8 10 11 
选择排序:0 1 2 3 3 4 5 8 10 11 
冒泡排序:0 1 2 3 3 4 5 8 10 11 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈皮的JavaLib

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

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

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

打赏作者

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

抵扣说明:

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

余额充值