冒泡排序的实现

来源:转载

 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 }


分享给朋友:
您可能感兴趣的文章:
随机阅读: