#include<iostream>
#include<vector>
using namespace std;
void SelectSort(vector<int> &array){
for(int i = 0; i < array.size(); i++){
int idxOfMin = i;
for(int j = i + 1; j < array.size(); j++){
if(array[j] < array[idxOfMin])
idxOfMin = j;
}
swap(array[i], array[idxOfMin]);
}
}
int main(int argc, char const *argv[])
{
vector<int> a1 = {5, 9, 0, 1, 3, 6, 4, 8, 2, 7};
SelectSort(a1);
for(auto &it : a1)
cout<<it<<' ';
cout<<endl;
return 0;
}
#include<iostream>
#include<vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
void SelectSort(vector<int> &array){
for(int i = 0; i < array.size(); i++){
int idxOfMin = i;
for(int j = i + 1; j < array.size(); j++){
if(array[j] < array[idxOfMin])
idxOfMin = j;
}
swap(array[i], array[idxOfMin]);
}
}
// 判斷array是否有序
bool isOrder(vector<int> &array){
for(int i = 1; i < array.size(); i++){
if(array[i] < array[i-1])
return false;
}
return true;
}
// 生成n個介於min,max之間的整型數
vector<int> RAND(int max, int min, int n) {
vector<int> res;
srand(time(NULL)); // 註釋該行以後,每次生成的隨機數都同樣
for(int i = 0; i < n; ++i) {
int u = (double)rand() / (RAND_MAX + 1) * (max - min) + min;
res.push_back(u);
}
return res;
}
// 使用200000個介於1,10000之間的數據進行測試
int main(int argc, char const *argv[]) {
vector<int> a = RAND(1, 10000, 200000);
clock_t start = clock();
SelectSort(a);
clock_t end = clock();
cout << "Time goes: " << (double)(end - start) / CLOCKS_PER_SEC << "sec" << endl;
bool sorted = isOrder(a);
cout<<sorted<<endl;
return 0;
}
Time goes: 184.319sec
1
[Finished in 185.6s]