Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341516
  • 博文数量: 2110
  • 博客积分: 18861
  • 博客等级: 上将
  • 技术积分: 24420
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-05 18:23
文章分类

全部博文(2110)

文章存档

2011年(139)

2010年(1971)

我的朋友

分类: Java

2010-10-27 11:07:09

 今天用JAVA写了两个排序算法,一个是冒泡排序,一个是选择排序,都是些基础的东西,但是觉得很有用。

  public class BubbleSort {

  public static void main(String[] args){

  int a[] = {2,5,9,3,7,8,4,1};

  for(int i=0;i

  for(int j=a.length-1;j>i;j--){

  if(a[j]

  int temp;

  temp = a[j];

  a[j] = a[j-1];

  a[j-1] = temp;

  System.out.println("完成一次交换");

  }else{

  System.out.println("不用交换");

  }

  }

  System.out.println(a[i]);

  }

  }

  }

  选择排序算法

  public class SelectSort {

  public static void main(String[] args){

  int[] a = {1,3,6,8,7,9,5,4,2};

  for(int i=0;i

  int pos = i;

  //找出最小值

  for(int j=i;j

  if(a[pos]>a[j]){

  pos = j;

  }

  }

  //把最小值弹出

  int temp;

  temp = a[i];

  a[i] = a[pos];

  a[pos] = temp;

  System.out.print(a[i]);

  }

  }

  }

阅读(549) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~