冒泡排序的理解
冒泡排序是最简单的的一种排序算法,也是要进行多趟,每一趟比出一个最大或最小的值,依次类推
比较简单
public class BubbleSort {
public static void main(String[] args) {
int[] a = {1,44,2,434,23,2,4,12,4,14,24,3,5,25} ;
BubbleSort sort = new BubbleSort() ;
sort.printBef(a) ;
sort.bubbleSort(a) ;
sort.printAft(a) ;
}
private void printAft(int[] a) {
System.out.println();
System.out.println("after bubblesort ,the order is:");
for(int i = 0;i
a[j]){
swap(a,i,j) ;
}
}
}
}
private void swap(int[] a, int i, int j) {
a[i] = a[i]+a[j] ;
a[j] =a[i]-a[j] ;
a[i] = a[i]-a[j] ;
}
private void printBef(int[] a) {
System.out.println("before bubbleSort ,the order is :");
for(int i = 0 ;i