Advertisement
HyperSensualNarwhal

Sorting bubble

Dec 23rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. int exchange(int &a, int &b);
  9.  
  10. void main()
  11. {
  12.     srand(time(NULL));
  13.     const int size = 10;
  14.     int arr[size];
  15.  
  16.     for (int i = 0; i < size; i++)
  17.     {
  18.         arr[i] = rand() % 10;
  19.         cout << arr[i] << "\t";
  20.     }
  21.  
  22.  
  23.     for (int i = 0; i < size; i++)
  24.     {
  25.         for (int j = 0; j < size; j++)
  26.         {
  27.             if (arr[j] < arr[j + 1])
  28.             {
  29.                 /*arr[j] = arr[j] + arr[j + 1];
  30.                 arr[j + 1] = arr[j] - arr[j + 1];
  31.                 arr[j] = arr[j] - arr[j + 1];*/
  32.                 exchange(arr[j], arr[j + 1]);   //int a = arr[j], int b = arr[j+1]
  33.             }
  34.         }
  35.     }
  36.  
  37.     cout << endl << endl;
  38.  
  39.     for (int i = 0; i < size; i++)
  40.     {
  41.         cout << arr[i] << "\t";
  42.     }
  43.  
  44.     cout << endl << endl;
  45. }
  46.  
  47. int exchange(int &a, int &b)
  48. {
  49.     int c = a;
  50.     a = b;
  51.     b = c;
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement