Advertisement
Guest User

Untitled

a guest
May 6th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7. void selectionSort(vector<T> &arr){
  8.     const int N = arr.size();
  9.     for(int i = 0; i < N; i++){
  10.         int minElemIndex = i;
  11.         for(int j = i + 1; j < N; j++)
  12.             if(arr[j] < arr[minElemIndex])
  13.                 minElemIndex = j;
  14.         swap(arr[i], arr[minElemIndex]);
  15.     }
  16. }
  17.  
  18. int main(){
  19.     int n;
  20.     cout << "Input amount of elements:\n";
  21.     cin >> n;
  22.     vector<int> a(n);
  23.     cout << "Input elements themselves:\n";
  24.     for(auto &i: a) cin >> i;
  25.     cout << "Array before sorting:\n";
  26.     for(auto &i: a) cout << i << " ";
  27.     selectionSort(a);
  28.     cout << "\nArray after sorting:\n";
  29.     for(auto &i: a) cout << i << " ";
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement