public class BubbleSort {
public static void bubbleSort(int[] list) {
boolean flag = true;
for( int n=1; n<list.length && flag; n++) {
flag = false;
for( int i=0; i<list.length-n; i++) {
if( list[i]>list[i+1]) {
int temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
flag = true;
}
}
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
int[] list = {2,3,2,5,6,1,-2,3,14,12};
bubbleSort(list);
for( int arr:list ) {
System.out.print(arr+" ");
}
}
}
Java之冒泡排序算法(三)
最新推荐文章于 2023-12-09 10:23:43 发布