public class TestSelectSort{
public
static void main(String[] args){
int [] sortArray = new
int []{ 5 , 7 , 4 , 2 , 9 , 8 , 3 , 6 };
System.out.println( "before sorting ,the numbers are:" );
show(sortArray);
selectSort(sortArray);
System.out.println( "after sorting,the numbers are:" );
show(sortArray);
}
public
static void selectSort( int [] intArray){
for ( int
i = 0 ;i<intArray.length;i++){
int
temp = i;
for ( int
j = intArray.length- 1 ;j >=i;j--){
if (intArray[j]< intArray[temp]){
temp = j;
}
}
if (i!=temp){
int
a = intArray[i];
intArray[i] = intArray[temp];
intArray[temp] = a;
}
}
}
public
static void show( int [] intArray){
for ( int
i= 0 ;i<intArray.length;i++){
System.out.print(intArray[i]+ "\t" );
}
System.out.println();
}
}
|