Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1-ary heap sort?

A while back we were given an assignment to write a c program that sorts an array of n numbers using a d-ary max-heap (a heap where each node has up to d children). The program needed to ask the user to input the value of d, a value between 2 and the size of the array. While I was checking my program I accidentally entered 1 as the value of d, and somehow the algorithm succeeded in sorting the array correctly using a 1-ary heap, although it took alot more time than normal values of d.

How is that possible? A 1-ary heap isn't even a heap it's just like a list, every node has only one child. Can anyone explain how this sorting could happen?

like image 836
Bob Avatar asked Dec 12 '10 11:12

Bob


People also ask

What is 3 ary max heap?

A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3].

What is a K ary heap?

(data structure) Definition: A complete tree where every node has a key more extreme (greater or less) than the key of its parent. Each node has k or fewer children.

Can heaps have more than 2 children?

The maximum number of children each node can have depends on the type of heap, but in many types it is at most two, which is known as a binary heap.

Why is heapsort O 1 space?

Only O(1) additional space is required because the heap is built inside the array to be sorted.


1 Answers

An 1-ary heap is still a heap, and satisfies all the invariants that are required by a heap sort:

  • The first element is the largest element.
  • Percolation can move the top element down to its correct position.

In practice, an 1-ary heap is a tree where every node has one child - this is also known as a singly linked list. Also, the heap constraint means the child node is smaller than the parent node. So, simply put, an 1-ary heap is a singly linked list sorted in reverse order.

Constructing the heap in the first place is equivalent to an insertion sort (percolate all items into the list one by one). Once this is done, removing the first element yields the largest element of all, and the subsequent percolation moves every item in the list up by one level.

like image 125
Victor Nicollet Avatar answered Sep 21 '22 16:09

Victor Nicollet