Advertisement
MeehoweCK

Untitled

Mar 27th, 2023
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void w(int j, int* A, int n)
  6. {
  7.     if(j > 0)
  8.         if(A[j] < A[j - 1])
  9.         {
  10.             int v = A[j];
  11.             A[j] = A[j - 1];
  12.             A[j - 1] = v;
  13.             w(j - 1, A, n);
  14.         }
  15. }
  16.  
  17. void w_2(int j, int* A, int n)
  18. {
  19.     while(j > 0)
  20.         if(A[j] < A[j - 1])
  21.         {
  22.             int v = A[j];
  23.             A[j] = A[j - 1];
  24.             A[j - 1] = v;
  25.             --j;
  26.         }
  27. }
  28.  
  29. void algorytm_s1(int* A, int n)
  30. {
  31.     for(int i = 1; i < n; ++i)
  32.         w(i, A, n);
  33. }
  34.  
  35. void algorytm_s2(int* A, int n)
  36. {
  37.     for(int i = n - 1; i > 0; --i)
  38.         w(i, A, n);
  39. }
  40.  
  41. void wypisz_tablice(int* tab, int n)
  42. {
  43.     for(int i = 0; i < 10; ++i)
  44.         cout << tab[i] << '\t';
  45.     cout << endl;
  46. }
  47.  
  48. int main()
  49. {
  50.     int A[10] = {2, 4, 6, 8, 10, 9, 7, 5, 3, 1};
  51.     wypisz_tablice(A, 10);
  52.  
  53.     algorytm_s2(A, 10);
  54.  
  55.     wypisz_tablice(A, 10);
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement