Heapsort

Heapsort.

 function heapSort(a, count) {
     var int start := count ÷ 2 - 1,
           end := count - 1

     while start ≥ 0
         sift(a, start, count)
         start := start - 1

     while end > 0
         swap(a[end], a[0])
         sift(a, 0, end)
         end := end - 1
 }

 function sift(a, start, count) {
     var int root := start, child

     while root * 2 + 1 < count {
         child := root * 2 + 1
         if child < count - 1 and a[child] < a[child + 1]
             child := child + 1
         if a[root] < a[child]
             swap(a[root], a[child])
             root := child
         else
             return
     }
 }
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s