Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void _swap(int &a, int &b) {
- swap(a, b);
- cout<<"swapped "<<a<<" & "<<b<<"\n";
- }
- void print(vector<int> a, int i) {
- cout << "iteration #" << i << " : ";
- for(auto x: a)
- cout << x << " ";
- cout<<endl;
- }
- void sel_sort(vector<int> a, int n) {
- int min;
- for (int i = 0; i < n - 1; i++) {
- min = i;
- for (int j = i + 1; j < n; j++) {
- if (a[j] < a[min])
- min = j;
- }
- if (min != i)
- _swap(a[min], a[i]);
- print(a, i+1);
- }
- cout << "\nselection sorted array: ";
- for (auto x: a) cout << x << " ";
- }
- int main()
- {
- int n;
- // cout << "enter size & elements of the array \n";
- cin >> n;
- vector<int> a(n);
- for (auto &x: a) cin >> x;
- sel_sort(a, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment