Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double min()
- {
- return heap[1];
- }
- void insert(double value)
- {
- int child = ++heap_size;
- int parent = child / 2;
- while (parent && heap[parent] > value)
- {
- heap[child] = heap[parent];
- child = parent;
- parent /= 2;
- }
- heap[child] = value;
- }
- int min_child(int parent)
- {
- int left = 2 * parent;
- int right = left + 1;
- if (left > heap_size) return 0;
- if (right > heap_size || heap[left] < heap[right])
- return left;
- return right;
- }
- void remove_min()
- {
- double last = heap[heap_size--];
- int x = 1;
- int c = katalog::min_child(1);
- while (c && heap[c] < last)
- {
- heap[x] = heap[c];
- x = c;
- c = katalog::min_child(c);
- }
- heap[x] = last;
- }
- void heapsort(double array[], int size)
- {
- if (size > HEAP_MAX) return;
- for (int i = 0; i < size; ++i)
- katalog::insert(array[i]);
- for (int i = 0; i < size; ++i)
- {
- array[i] = min();
- katalog::remove_min();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement