Advertisement
Vladislav_Bezruk

swap O(1)

Oct 1st, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define N 10
  4.  
  5. using namespace std;
  6.  
  7. void swap(int** a, int** b) { //вважається що a[] і b[] мають однаковий розмір!!!
  8.     int* bkp = *a;
  9.    
  10.     *a = *b;
  11.     *b = bkp;
  12. }
  13.  
  14. int main() {
  15.  
  16.     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  17.     int b[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  18.    
  19.     for (int i = 0; i < N; i++) {
  20.         cout << a[i] << ' ';
  21.     }
  22.     cout << endl;
  23.    
  24.     for (int i = 0; i < N; i++) {
  25.         cout << b[i] << ' ';
  26.     }
  27.     cout << endl;
  28.    
  29.     cout << endl << endl;
  30.    
  31.     swap(a, b);
  32.    
  33.     for (int i = 0; i < N; i++) {
  34.         cout << a[i] << ' ';
  35.     }
  36.     cout << endl;
  37.    
  38.     for (int i = 0; i < N; i++) {
  39.         cout << b[i] << ' ';
  40.     }
  41.     cout << endl;
  42.    
  43.    
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement