Advertisement
MaximPro

Сортировка пузырьком с временим.

Dec 22nd, 2014
171
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 <ctime>
  3. #include <algorithm>
  4.  
  5.  
  6.  
  7.  
  8.  
  9. using namespace std;
  10. void bubble(int *a, int n) {
  11.    
  12.     for (int i=n; i>=0; i--) {
  13.         for (int j=0; j<i; j++) {
  14.             if (a[j]>a[j+1]) {
  15.                 swap(a[j],a[j+1]);
  16.             }
  17.         }
  18.     }
  19. }
  20.  
  21.  
  22.  
  23. int main(int argc, char** argv) {
  24.    
  25.      int n;
  26.      cin >> n;
  27.      int a[n];
  28.      for (int i=0; i<=n; i++) {
  29.        a[i]=rand() % n;
  30.      }
  31.      unsigned start_time = clock();
  32.      bubble(a,n);
  33.      unsigned end_time = clock();
  34.      for ( int i=0; i<=n; i++) {
  35.         cout <<  "   "  << a[i];
  36.      }
  37.      cout << endl;
  38.      cout << " Time: " <<  end_time-start_time << " mc " << endl;
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement