统计信息

  • 访问数:11140
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q16

2008-04-17 13:41:42

天气: 晴朗 心情: 高兴

/*filename:P6Q16
 *Name:Wang Chaoran
Description:
(Revising selection sort) In §6.8, you used selection sort to sort an array.
The selection sort method repeatedly finds the largest number in the current array
and swaps it with the last number in the array. Rewrite this example by finding
the smallest number and swapping it with the first number in the array.
*/
class P6Q16{
  public static void main(String[] args){
  final int length = 100;
  int[] number=new int[length];
  for(int i=0;i<number.length;i++)
    number[i]=(int)(Math.random()*(length+1));
  print(number);
  selectionSort(number);
  print(number);
  }
  static void print(int[] array){
    for(int i=0;i<array.length;i++)
      System.out.print(array[i]+" ");
    System.out.println();
}
  static void selectionSort(int[] array){
  
    for(int i=0;i<array.length;i++){
        int current = array[array.length-1];
        int index=array.length-1;
      for(int j = i;j<array.length-1;j++){
        if(array[j]<current){
          current = array[j];
          index = j;
        }
      }
        if(index!=i){
        array[index]=array[i];
        array[i]=current;
        }
  }
  }
}
 
加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd