Processing math: 100%

Heap Sort solved by 718

Feb. 21, 2014, 4:25 p.m. by Rosalind Team

Topics: Sorting

Problem

The heap sort algorithm first transforms a given array into a max heap (recall the problem “Building a Heap”). It then repeats the following two simple steps n1 times:

Given: A positive integer n105 and an array A[1..n] of integers from 105 to 105.

Return: A sorted array A.

Sample Dataset

9
2 6 7 1 3 5 4 8 9

Sample Output

1 2 3 4 5 6 7 8 9

Running timeclick to expand

Transforming a given array into a heap is done in linear time, O(n). The next stage requires n1 "sift-down" operations and hence take time O(nlogn). Hence the overall running time of the heap sort algorithm is O(nlogn).

Visualization

To see how the heap sort algorithm performs on various types of arrays use the following visualization by David R. Martin: http://www.sorting-algorithms.com/heap-sort. The visualization by David Galles also shows how the heap sort algorithm performs on a random array: http://www.cs.usfca.edu/~galles/visualization/HeapSort.html.

Please login to solve this problem.

Welcome to Rosalind!

Rosalind is a platform for learning bioinformatics through problem solving.
Please login with Google/Twitter/Facebook or register a new account.