pmcgee

Bubblesort with gsl/span

Sep 1st, 2020 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <gsl/span>
  5.  
  6. typedef std::vector<int>    IntVec;
  7. typedef gsl::span<int>      IntSpan;
  8.  
  9. // - - - - - - - - - - - - -
  10.  
  11. void OldBubble(IntVec& v) {
  12.     for(int a=0; a<10; ++a)  {  //using bubble sort algorithm here
  13.         for(int b=a+1; b<10; ++b)   {
  14.            if (v[a] > v[b])         {  std::swap(v[a],v[b]);  }
  15.          }
  16.     }
  17. }    
  18.  
  19.  
  20. void SpanBubble(IntSpan v)  {
  21.     int i=0;  
  22.     for(int& a : v) {
  23.         ++i;
  24.         for(auto& b : v.subspan(i) )    {
  25.             if ( a > b)                 {  std::swap(a,b);  }  
  26.          }  
  27.     }          
  28. }            
  29.  
  30. // - - - - - - - - - - - - -
  31.  
  32. //int arr [10] {3,7,6,2,8,4,5,5,9,7};
  33. IntVec vec     {3,7,6,2,8,4,5,5,9,7};
  34. IntVec vek = vec;
  35.  
  36. int main() {
  37.     SpanBubble  (vec);
  38.     OldBubble   (vek);
  39.  
  40.     return 0;
  41. }
  42.  
Add Comment
Please, Sign In to add comment