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

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();

相关推荐
卷福同学2 小时前
【养虾日记】Openclaw操作浏览器自动化发文
人工智能·后端·算法
春日见3 小时前
如何入门端到端自动驾驶?
linux·人工智能·算法·机器学习·自动驾驶
图图的点云库3 小时前
高斯滤波实现算法
c++·算法·最小二乘法
回到原点的码农3 小时前
Spring Data JDBC 详解
java·数据库·spring
gf13211113 小时前
python_查询并删除飞书多维表格中的记录
java·python·飞书
zb200641204 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot
一叶落4384 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
一勺菠萝丶4 小时前
Flowable + Spring 集成踩坑:流程结束监听器查询历史任务为空 & 获取不到审批意见
java·数据库·spring
jwn9994 小时前
Spring Boot 整合 Keycloak
java·spring boot·后端
宁波阿成4 小时前
OpenClaw 在 Ubuntu 22.04.5 LTS 上的安装与问题处理记录
java·linux·ubuntu·openclaw·龙虾