/**
* 選擇排序(簡單選擇排序)
* @author Cinn
*
*/
public class selectSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array= {48,58,50,98,69,51,27,99,100};
selectsort(array);
printArray(array);
}
/**
* 簡單選擇排序算法接口
* @param a
*/
public static void selectsort(int[] a){
for (int i = 0; i < a.length; i++) {
int j = i+1;
int position = i;
int temp = a[i];
for (; j < a.length; j++) {
if(a[j]<temp){
temp = a[j];
position = j;
}
}
a[position] = a[i];
a[i] = temp;
}
* 列印數組
* @param array
public static void printArray(int[] array){
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
選擇排序是不穩定的排序、時間複雜度為T(o) = O(n^2)