Advertisement
Guest User

merge sort

a guest
Jan 18th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const short size = 13;
  9. short size2 = 0;
  10. //short arr[size]{ 1,3,0,4,2,9,7,6,5,8 };
  11. short arr[size]{ 75, 11, 17, 78, 75, 2, 98, 27, 56, 4, 99, 16, 65 };
  12.  
  13. /*short arr[size]{};
  14. srand(time(NULL));
  15.  
  16. for (short i = 0; i < size; i++)
  17. {
  18. arr[i] = rand() % 101;
  19. }*/
  20.  
  21. for (short a : arr)
  22. {
  23. cout << a << ' ';
  24. }
  25. cout << endl;
  26.  
  27. short counter = 0;
  28. for (short i = 2;; i *= 2)
  29. {
  30. for (short j = 0;; j += i)
  31. {
  32. short temp = 0;
  33. for (short k = j; k + 1 <= j + i; k++)
  34. {
  35. short min = k;
  36. for (short m = k + 1; m < j + i && m < size; m++)
  37. {
  38. if (arr[min] > arr[m])
  39. {
  40. min = m;
  41. }
  42. }
  43. if (min != k)
  44. {
  45. temp = arr[min];
  46. arr[min] = arr[k];
  47. arr[k] = temp;
  48. }
  49. counter++;
  50. }
  51. if (j > size - 1)
  52. {
  53. break;
  54. }
  55. }
  56.  
  57. if (i > size - 1)
  58. {
  59. break;
  60. }
  61. }
  62. for (short a : arr)
  63. {
  64. cout << a << ' ';
  65. }
  66. cout << endl << counter << endl;
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement