直接上代码,看注释就能懂
时间复杂度:
最优O(n^2),
最坏时间复杂度为 O(n^2) ,
平均时间复杂度为O(n^2) 。
空间复杂度:O(1)。
不稳定
package sort;
public class SimpleSelectSort {
public static void simpleSelectSort(int array[]) {
int temp = array[0];
int index=0;
for(int j = 0;j<array.length-1;j++) {
temp=array[j];
index= j;
for(int i = j+1;i<array.length;i++) {
//从已经排好的序列的下一位和下一位的下一位开始比较
if(array[i]<temp) {
//找到最小的值是多少。
temp = array[i];
index = i;//记录的目前最小的值的下标
}
}
//将找到的最小值和从已经排好的序列的下一位调换位置
array[index]=array[j];
array[j]=temp;
}
}
public static void main(String[] args) {
int a[]= {6 ,1, 2, 7, 9, 3, 4, 5, 10, 8};
simpleSelectSort(a);
for (int i : a) {
System.out.print(i+" ");
}
}
}