Saturday, February 25, 2012

Something about Heapsort

Heapsort has two pleasant properties

  1. an average and worst-case O(N*LogN) complexity
  2. in place algorithm, no extra space is needed.

Heapsort has two steps:

  1. first make a max heap (or min heap), this operation takes O(N)
  2. pop the root of the heap, swap it with the last element in the heap (the last leaf), do heapify on the elements rangeing from 1 to N-1. Basically, we build a new max (min) heap on the elements rangeing from 1 to N-1
  3. repeat the step 2

1 comment: