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

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;

}

// 依次让00,01,02,...,0n-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();

相关推荐
Han.miracle5 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
Le1Yu6 小时前
分布式事务以及Seata(XA、AT模式)
java
寒山李白6 小时前
关于Java项目构建/配置工具方式(Gradle-Groovy、Gradle-Kotlin、Maven)的区别于选择
java·kotlin·gradle·maven
mit6.8246 小时前
前后缀分解
算法
你好,我叫C小白7 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
无妄无望7 小时前
docker学习(4)容器的生命周期与资源控制
java·学习·docker
MC丶科7 小时前
【SpringBoot 快速上手实战系列】5 分钟用 Spring Boot 搭建一个用户管理系统(含前后端分离)!新手也能一次跑通!
java·vue.js·spring boot·后端
千码君20168 小时前
React Native:从react的解构看编程众多语言中的解构
java·javascript·python·react native·react.js·解包·解构
夜白宋9 小时前
【word多文档docx合并】
java·word
@yanyu6669 小时前
idea中配置tomcat
java·mysql·tomcat