Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- template <class T>
- void selectionSort(vector<T> &arr){
- const int N = arr.size();
- for(int i = 0; i < N; i++){
- int minElemIndex = i;
- for(int j = i + 1; j < N; j++)
- if(arr[j] < arr[minElemIndex])
- minElemIndex = j;
- swap(arr[i], arr[minElemIndex]);
- }
- }
- int main(){
- int n;
- cout << "Input amount of elements:\n";
- cin >> n;
- vector<int> a(n);
- cout << "Input elements themselves:\n";
- for(auto &i: a) cin >> i;
- cout << "Array before sorting:\n";
- for(auto &i: a) cout << i << " ";
- selectionSort(a);
- cout << "\nArray after sorting:\n";
- for(auto &i: a) cout << i << " ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement