Advertisement
ilnazEPTA

Untitled

May 21st, 2020
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void heapSort(int arr[], int n)
  5. {
  6.  
  7. for (int j = 0; j < n; j++)
  8. {
  9. for (int i = n / 2 - 1 - j / 2; i > -1; i--)
  10. {
  11. if (2 * i + 2 < n - 1 - j)
  12. {
  13. if (arr[2 * i + 1] > arr[2 * i + 2])
  14. {
  15. if (arr[i] < arr[2 * i + 1])
  16. {
  17. swap(arr[i], arr[2 * i + 1]);
  18. }
  19. }
  20. else
  21. if (arr[i] < arr[2 * i + 2])
  22. {
  23. swap(arr[i], arr[2 * i + 2]);
  24.  
  25. }
  26. }
  27. else
  28. if (2 * i + 1 <= n - 1 - j)
  29. if (arr[i] < arr[2 * i + 1])
  30. swap(arr[i], arr[2 * i + 1]);
  31. }
  32. swap(arr[0], arr[n - 1 - j]);
  33. }
  34. }
  35.  
  36.  
  37.  
  38. int main()
  39. {
  40. int a[] = { 5,4,9,1,3,2,12,7,6,8 };
  41. for (int i = 0; i < 10; i++)
  42. cout << a[i] << ' ';
  43. cout << endl;
  44. heapSort(a, 10);
  45. for (int i = 0; i < 10; i++)
  46. cout << a[i] << ' ';
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement