Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Процедура просеивания пирамиды
- void Sift(int *arr, int start, int length)
- {
- int id_left, id_right, max, t, i = start;
- char flag = 1;
- while ((i < length) && flag)
- {
- if (2 * i + 1 < length)
- {
- if (2 * i + 1 == length - 1)
- {
- max = 2 * i + 1;
- }
- else
- {
- id_left = 2 * i + 1;
- id_right = (i + 1) * 2;
- max = (arr[id_left] >= arr[id_right]) ? id_left : id_right;
- }
- if (arr[i] <= arr[max])
- {
- t = arr[i];
- arr[i] = arr[max];
- arr[max] = t;
- }
- i = max;
- }
- else
- flag = 0;
- }
- }
- // Процедура пирамидальной сортировки
- void HeapSort(int *arr, int length)
- {
- for (int i = length / 2; i >= 0; i--)
- {
- Sift(arr, i, length);
- }
- while (length > 0)
- {
- int t = arr[length - 1];
- arr[length - 1] = arr[0];
- arr[0] = t;
- length--;
- Sift(arr, 0, length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment