csansoon

P10.04 P81602 Selection sort

Dec 23rd, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. void selection_sort(vector<double>& v, int m) {
  7.         if (m > 0) {
  8.                 double max = v[0];
  9.                 int pos = 0;
  10.                 for (int i = 1; i <= m; ++i) {
  11.                         if (v[i] > max) {
  12.                                 max = v[i];
  13.                                 pos = i;
  14.                         }
  15.                 }
  16.                 v[pos] = v[m];
  17.                 v[m] = max;
  18.                 return selection_sort(v, m-1);
  19.         }
  20. }
  21.  
  22. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment