Advertisement
Swiftkill

bubble

Apr 1st, 2019
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <list>
  4. #include <iostream>
  5. #include <chrono>
  6. #include <ctime>
  7.  
  8. template <class InIt>
  9. void bubblesort(InIt start, InIt stop)
  10. {
  11.     InIt k = stop, j_1;
  12.     for (auto i = start; i != stop; ++i)
  13.     {
  14.         for (auto j = std::next(start, 1); j != k; ++j)
  15.         {
  16.             j_1 = std::prev(j, 1);
  17.             if (*j_1 > *j)
  18.             {
  19.                 auto temp = *j;
  20.                 *j = *j_1;
  21.                 *j_1 = temp;
  22.                 //std::swap(*j,*j_1);
  23.             }
  24.         }
  25.         --k;
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.   std::list<int> l;
  32.  
  33.   for (int i = 0; i != 10000; ++i)
  34.     l.push_front(i);
  35.  
  36.  
  37.   //for (auto i : l) std::cout << i << ' '; std::cout << std::endl;
  38.  
  39.   std::chrono::time_point<std::chrono::system_clock> start, end;  
  40.  
  41.   start = std::chrono::system_clock::now();
  42.   bubblesort(l.begin(), l.end());
  43.   end = std::chrono::system_clock::now();
  44.   std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() / 1000.0
  45.     << " sec" << std::endl;
  46.  
  47.  
  48.   //for (auto i : l) std::cout << i << ' ';  std::cout << std::endl;
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement