冒泡排序
package com.uriel.sort;
public class TestBubbleSort {
/**
* @param arr
* @description: 冒泡排序 时间复杂度 O(n²)
* @date: 2022/5/21 17:24
*/
public static void bubbleSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
swap(arr, i, j);
}
}
}
}
public static void swap(int[] arr, int i, int minIndex) {
int tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
}
本文来自博客园,作者:wjxuriel,转载请注明原文链接:https://www.cnblogs.com/my-blog-site/p/16306393.html

浙公网安备 33010602011771号