交换排序 错在哪里。

作者在 2010-06-20 23:33:50 发布以下内容

public class Swap {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //交换法
  
  int[] score = new int[100];
  //初始化数组
  for (int i = 0; i <= score.length - 1; i++) {
   score[i] = (int)(Math.random() * 100);
  }
  
  int temp ;//中间变量
  int i ;
  int j ;
  for (i = 0; i < score.length - 1; i++) {
   int min = i;//假设最小值
   
   //找最小值
   for (j = i + 1; j <= score.length - 1; j++) {
    if (score[j] < min) {
     min = j;
    }
   }
   //交换最小值的位置
   if (min != i) {
    j--;
    temp = score[i];
    score[i] = score[j];
    score[j] = temp ;
   }
  }
  
  System.out.println("排序后的数组:");
  for (int k = 0; k <= score.length - 1; k++) {
   if (k % 10 == 0) {
    System.out.println();
   }
   System.out.print(score[k]+"\t");
  }
  
 }
 
}
默认分类 | 阅读 730 次
文章评论,共0条
游客请输入验证码
浏览730次
文章分类
文章归档
最新评论