Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. void init_array(int* A, int n);
  4. void print_array(int* A, int n);
  5. void swap(int* a, int* b, int temp);
  6. void shaker_sort(int* A, int n);
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12. srand(123456);
  13. int const n = 10;
  14. int A[n];
  15. init_array(A, n);
  16. print_array(A, n);
  17. shaker_sort(A, n);
  18.  
  19.  
  20. }
  21.  
  22. void init_array(int* A, int n)
  23. {
  24. for (int i = 0; i < n; i++)
  25. {
  26. A[i] = rand() % 40;
  27. }
  28. return;
  29. }
  30.  
  31. void print_array(int* A, int n)
  32. {
  33. for (int i = 0; i < n; i++)
  34. {
  35. printf("%-4d", A[i]);
  36.  
  37. }
  38. printf("\n");
  39. return;
  40. }
  41.  
  42. void swap(int* a, int* b)
  43. {
  44. int temp = *a;
  45. *a = *b;
  46. *b = temp;
  47.  
  48.  
  49. return ;
  50.  
  51. }
  52.  
  53.  
  54. void shaker_sort (int* A, int n)
  55. {
  56. for (int i = 0; i < n-1; i++)
  57. {
  58. for (int j = 0; j < n - (i + 1); j++)
  59. {
  60. if (A[j] > A[j + 1])
  61. {
  62. swap(A+j, A+j + 1);
  63. print_array(A, n);
  64. }
  65. /*for (int z = n - (i+j+ 1); z > 0; i--)
  66. {
  67. if (A[z + 1] < A[z])
  68. { swap(A+z, A+z+1);
  69. print_array(A, n);
  70.  
  71.  
  72. }
  73. }*/
  74.  
  75.  
  76. }
  77. for (int z = n - (i + 1); z > 0; z--)
  78. {
  79. if (A[z + 1] < A[z])
  80. {
  81. swap(A + z, A + z + 1);
  82. print_array(A, n);
  83.  
  84.  
  85. }
  86. }
  87. }
  88. return;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement