C++代码:
#include <iostream>
#include <algorithm>
using namespace std;
void selectionSort(int arr[], int n){
for(int i = 0 ; i < n ; i ++){
// 寻找[i, n)区间里的最小值
int minIndex = i;
for( int j = i + 1 ; j < n ; j ++ )
if( arr[j] < arr[minIndex] )
minIndex = j;
swap( arr[i] , arr[minIndex] );
}
}
int main() {
int a[10] = {10,9,8,7,6,5,4,3,2,1};
selectionSort(a,10);
for( int i = 0 ; i < 10 ; i ++ )
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
Python代码:
#生成[frist, last]的连贯但乱序的待排列的数据
def randomSeq(frist, last):
seq = [i for i in xrange(frist, last+1)]
lenSeq = len(seq)
rSeq = []
for i in xrange(lenSeq):
rSeq.append(seq.pop(randint(0, lenSeq-1-i)))
return rSeq
--------------
In[]:randomSeq(1, 10)
Out[]:[6, 8, 4, 10, 5, 2, 9, 1, 7, 3]