直接选择排序(Straight Select Sorting) 也是一种简单的排序方法,它的基本思想是:第一次从R[0]~R[n-1]中选取最小值,与R[0]交换,第二次从R[1]~R[n-1]中选取最小值,与R[1]交换,....,第i次从R[i-1]~R[n-1]中选取最小值,与R[i-1]交换,.....,第n-1次从R[n-2]~R[n-1]中选取最小值,与R[n-2]交换,总共通过n-1次,得到一个按排序码从小到大排列的有序序列·
package algorithm;
public class SelectSort {
public static void main(String[] args) {
int array[] = {3,7,2,4,1};
selectSort(array);
}
public static void selectSort(int array[]) {
int i,j,n;
int temp;
for(i = 0; i<array.length;i++){
temp = array[i];
n = i;
for(j = i+1;j<array.length;j++){
if(array[j]<temp){
temp = array[j];
n = j;
}
}
array[n] = array[i];
array[i] = temp;
out(array);
}
}
public static void out(int array[]) {
int i ;
for (i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
System.out.println();
}
}
运行结果:
1 7 2 4 3
1 2 7 4 3
1 2 3 4 7
1 2 3 4 7
1 2 3 4 7