#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<xfunctional>
using namespace std;
void select(vector<int>& arr){
for (int i = 0; i < arr.size()-1; i++){
int min_num = INT_MAX;
int index = -1;
for (int j = i + 1; j < arr.size(); j++){
if (arr[j] < min_num){
min_num = arr[j];
index = j;
}
}
if (arr[index]<arr[i]) swap(arr[index],arr[i]);
}
}
int main(){
int n;
cout << "Input the size(>=1) of the array:";
cin >> n;
vector<int> arr(n,0);
cout << "Input the array element:";
for (int i = 0; i < n; i++) cin >> arr[i];
select(arr);
for (int i = 0; i < arr.size(); i++) cout << arr[i] << " ";
cout << endl;
system("pause");
return 0;
}
For each iteration,we need to find the smallest element among the remaining numbers,so the best case ,worst case and average case running times are all O(n^2)