Programs = Algorithm + Data Structures
算法对我们平时的开发起着至关重要的作用,这是最简单的选择排序:
void selectSort(int[] a){
int len = a.length;
int minIndex;
for (int i = 0; i < len - 1; i++) { //cycle n-1 times
minIndex = i;
int tmp;
for (int j = i+1; j < len; j++) {
if(a[j]<a[minIndex]){
minIndex = j;
}
}
if (minIndex != i){ //just change the index of these two values
tmp = a[i];
a[i] = a[minIndex];
a[minIndex] =tmp;
}
}
System.out.println(Arrays.toString(a));
}