Einzelnen Beitrag anzeigen

tigerman33

Registriert seit: 30. Jul 2005
Ort: München
423 Beiträge
 
Delphi 2005 Professional
 
#6

Re: Build Heap And Heapsort Without Array (Sift-operation)

  Alt 2. Mai 2006, 07:03
Wikipedia article explaining heapsort/sift:
Zitat von Wikipedia:
Code:
function sift(a, start, count) {
     var int root := start, child

     while root * 2 + 1 < count { 
         child := root * 2 + 1 // Ignore this--you can access a node's children much easier! :)

         if child < count - 1 and a[child] < a[child + 1] // Choose the larger of the sons, if there are two

             child := child + 1

         if a[root] < a[child] // If root node is too small...
             swap(a[root], a[child]) // Swap it with the previously determined larger son
             root := child // ...and keep track of where it is now.
               // This way, on the next loop iteration the subtree that has the just-moved node as its root will be sifted into
               // Another possible way here would have been to use recursion
         else
             return
     }
 }
PS: Yeah, I know this isn't really C-code. Just wanted the syntax highlighting...
Christian
Der Computer hilft mir, Probleme zu lösen, die ich ohne Computer nicht hätte.
  Mit Zitat antworten Zitat