package com.sort;
//冒泡排序:O(n^2)
public class BubbleSort {
public static void bubbleSort(int[] a) {
for (int i = 1; i < a.length; i++) {// 表示次数
int j = a.length - 1;
while (j >= i) {
if (a[j] < a[j-1]) {
swap(a,j,j-1);
}
j--;
}
}
}
private static void swap(int[] a, int j, int i) {
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
public static void main(String[] args) {
int[] a = new int[]{6,5,4,3,2,1};
bubbleSort(a);
for(int array:a){
System.out.println(array);
}
}
}
交换排序之冒泡排序
最新推荐文章于 2023-06-06 20:45:43 发布