冒泡排序的实现

 1 public class BubbleSortDemo {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int []ary=new int[]{2,1,4,3,7,5,6,9};
9 ary=bubblesort(ary);
10 for (int i = 0; i < ary.length; i++) {
11 System.out.print(ary[i] + " ");
12 }
13 }
14
15 public static int[] bubblesort(int []ary)
16 {
17 for(int i=0;i<ary.length-1;i++)
18 {
19 for(int j=0;j<ary.length-i-1;j++)
20 {
21 if(ary[j]>ary[j+1])
22 {
23 int temp = ary[j];
24 ary[j] = ary[j+1];
25 ary[j+1] = temp;
26 }
27 }
28 }
29 return ary;
30 }
31 }

  

posted @ 2011-07-26 09:18  牧涛  阅读(207)  评论(0编辑  收藏  举报