数据结构与算法
选择排序
import java.util.Scanner;
public class SelectSort {
public static void sort(int [] arr){
int n =arr.length;
if(arr == null || n<2){
return ;
}
for(int i =0;i<n;i++){
int minVlueIndex=i;
for(int j = i+1;j<n;j++){
minVlueIndex = arr[j]< arr[minVlueIndex] ? j : minVlueIndex;
}
swap( arr,i,minVlueIndex);
}
for(int a:arr)
System.out.print(" "+a);
}
public static void swap(int [] arr,int i,int j){
int tep = arr[i];
arr [i] = arr[j];
arr[j] = tep;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n =scanner.nextInt();
Scanner sc= new Scanner(System.in);
int [] arr = new int[n];
for(int i =0;i<arr.length;i++){
arr[i]=sc.nextInt();
System.out.print(" " + arr[i]);
}
sort(arr);
}
}