交换排序——冒泡排序
冒泡排序是将相邻两个元素进行比较,如果是从前往后面开始那么进行一趟比较之后最轻或最最重的元素就在后面面了,然后前n-1个数再次比较,依次下去。
//冒泡排序 public class BubbleSort { public void bubbleSort(int[] r,int low,int high) { int n=high-low+1; for(int i=1;i<n;i++) for(int j=low;j<=high-i;j++) if(r[j]>r[j+1]) { int temp=r[j]; r[j]=r[j+1]; r[j+1]=temp; } } public static void main(String[] args) { BubbleSort b=new BubbleSort(); int[] a={4,8,3,6,9,7,5,2,1}; b.bubbleSort(a,0,8); for(int i=0;i<a.length;i++) System.out.println(a[i]); } }