我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*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;
}
}
}
}
导入论坛查看(43)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing