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

简介: 冒泡排序 选择排序 插入排序

@[TOC]

插入排序

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 
相关文章
|
4月前
|
算法 搜索推荐 C++
C++017-C++冒泡排序与插入排序
C++017-C++冒泡排序与插入排序
C++017-C++冒泡排序与插入排序
|
6月前
冒泡排序和选择排序
冒泡排序和选择排序
|
10月前
|
存储 搜索推荐 Java
【排序算法】冒泡排序、选择排序、插入排序
【排序算法】冒泡排序、选择排序、插入排序
74 0
【排序算法】冒泡排序、选择排序、插入排序
|
10月前
|
算法 搜索推荐
冒泡排序与选择排序详解
冒泡排序与选择排序详解
|
11月前
|
搜索推荐
冒泡排序,选择排序,直接插入排序
🐰冒泡排序 🐰选择排序 🐰直接插入排序
|
算法 搜索推荐 索引
03_1排序算法:冒泡排序、选择排序、插入排序
03_1排序算法:冒泡排序、选择排序、插入排序
134 0
03_1排序算法:冒泡排序、选择排序、插入排序
冒泡排序、插入排序、选择排序
冒泡排序、插入排序、选择排序
冒泡排序,选择排序,插入排序
冒泡排序,选择排序,插入排序

热门文章

最新文章