KAY

人之所以能,是相信能。
posts - 6, comments - 5, trackbacks - 0, articles - 11
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

JAVA冒泡排序

Posted on 2008-02-26 08:54 KAY 阅读(414) 评论(0)  编辑  收藏

public class SortTest {
 public static void main(String[] args) {
  int[] values = { 3, 1, 6, 2, 9, 0, 7, 4, 5 };
  sort(values);
  for (int i = 0; i < values.length; i++) {
   System.out.println(values[i]);
  }
 }

 public static void sort(int[] values) {
  int temp;
  for (int i = 0; i < values.length; i++) {
   for (int j = 0; j < values.length - i - 1; j++) {
    if (values[j] > values[j + 1]) {//可以在这里调整排序是ASC or DESC
     temp = values[j];
     values[j] = values[j + 1];
     values[j + 1] = temp;
    }
   }
  }
 }
}


只有注册用户登录后才能发表评论。


网站导航:
 
QQ:
254653340
MSN:
xf_chouyang@hotmail.com