Advertisement
Kwwiker

Пузырёк

May 20th, 2021
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. void printArray(int *arr, int n)
  7. {
  8.     for (int i = 0; i < n; i++)
  9.         cout << arr[i] << " ";
  10.     cout << endl;
  11. }
  12.  
  13. void fillRandArray(int *arr, int n)
  14. {
  15.     for (int i = 0; i < n; i++)
  16.         arr[i] = rand();
  17. }
  18.  
  19. long long exchangeSort(int *arr, int n)
  20. {
  21.     long long compare = 0, move = 0;
  22.     for (int i = 0; i < n - 1; i++)
  23.         for (int j = 0; j < n - i - 1; j++) {
  24.             compare++;
  25.             if (arr[j] > arr[j + 1]) {
  26.                 move++;
  27.                 int temp = arr[j];
  28.                 arr[j] = arr[j + 1];
  29.                 arr[j + 1] = temp;
  30.             }
  31.         }
  32.     return compare + move;
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. int main()
  43. {
  44.     srand(time(0));
  45.     const long long n = 100; //100, 1000, 10000, 100000
  46.     int *arr = new int[n];
  47.     fillRandArray(arr, n);
  48.     long long start = clock();
  49.     cout << exchangeSort(arr, n) << endl;
  50.     long long end = clock();
  51.     cout << end - start << endl;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement