选择排序概念:首先找到最先的元素然后和第一个元素交换,然后在剩下的元素中找到最小的元素与第二个元素替换,以此类推。
import java.util.*;
public class SelectionSort {
public int[] selectionSort(int[] a, int n) {
// write code here
for(int i=0;i<n;i++){
int min = i;
for(int j=i+1;j<n;j++){
if(a[j]<a[min]){
min = j;
}
}
if(i!=min){
int temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
return a;
}
}