冒泡排序:
基本思想:比较相邻的元素大小,将小的前移,大的后移(或者反过来。)
/** * 冒泡排序 * @author Ant * */
public class BubbleSort {
/** * 基本思想:比较相邻的元素大小,将小的前移,大的后移 * @param a * @return */
public static void bubbleSort(int a[]){
for(int i = 0; i < a.length-1; i++){
for(int j = 1; j < a.length - i; j++){
if(a[j]<a[j-1]){
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int a [] = {32, 20, 22, 6, 2, 4, 1, 5, 9};
BubbleSort.bubbleSort(a);
for(int i = 0; i < a.length; i++){
System.out.println(a[i]+" ");
}
}
}