Sorting algorithms/Heapsort: Difference between revisions
(added python translated from ruby) |
(Ada) |
||
Line 46: | Line 46: | ||
'''return''' |
'''return''' |
||
Write a function to sort a collection of integers using heapsort. |
Write a function to sort a collection of integers using heapsort. |
||
=={{header|Ada}}== |
|||
This implementation is a generic heapsort for unconstrained arrays. |
|||
<lang Ada>generic |
|||
type Element_Type is private; |
|||
type Index_Type is (<>); |
|||
type Collection is array(Index_Type range <>) of Element_Type; |
|||
with function "<" (Left, right : element_type) return boolean is <>; |
|||
procedure Generic_Heapsort(Item : in out Collection);</lang> |
|||
<lang Ada>procedure Generic_Heapsort(Item : in out Collection) is |
|||
procedure Swap(Left : in out Element_Type; Right : in out Element_Type) is |
|||
Temp : Element_Type := Left; |
|||
begin |
|||
Left := Right; |
|||
Right := Temp; |
|||
end Swap; |
|||
procedure Sift_Down(Item : in out Collection) is |
|||
Root : Integer := Index_Type'Pos(Item'First); |
|||
Child : Integer := Index_Type'Pos(Item'Last); |
|||
Last : Integer := Index_Type'Pos(Item'Last); |
|||
begin |
|||
while Root * 2 + 1 <= Last loop |
|||
Child := Root * 2 + 1; |
|||
if Child + 1 <= Last and then Item(index_Type'Val(Child)) < Item(Index_Type'Val(Child + 1)) then |
|||
Child := Child + 1; |
|||
end if; |
|||
if Item(Index_Type'Val(Root)) < Item(Index_Type'Val(Child)) then |
|||
Swap(Item(Index_Type'Val(Root)), Item(Index_Type'Val(Child))); |
|||
Root := Child; |
|||
else |
|||
exit; |
|||
end if; |
|||
end loop; |
|||
end Sift_Down; |
|||
procedure Heapify(Item : in out Collection) is |
|||
First_Pos : Integer := Index_Type'Pos(Index_Type'First); |
|||
Last_Pos : Integer := Index_Type'Pos(Index_type'Last); |
|||
Start : Index_type := Index_Type'Val((Last_Pos - First_Pos + 1) / 2); |
|||
begin |
|||
loop |
|||
Sift_Down(Item(Start..Item'Last)); |
|||
if Start > Index_Type'First then |
|||
Start := Index_Type'Pred(Start); |
|||
else |
|||
exit; |
|||
end if; |
|||
end loop; |
|||
end Heapify; |
|||
Last_Index : Index_Type := Index_Type'Last; |
|||
begin |
|||
Heapify(Item); |
|||
while Last_Index > Index_Type'First loop |
|||
Swap(Item(Last_Index), Item(Item'First)); |
|||
Last_Index := Index_Type'Pred(Last_Index); |
|||
Sift_Down(Item(Item'First..Last_Index)); |
|||
end loop; |
|||
end Generic_Heapsort;</lang> |
|||
Demo code: |
|||
<lang Ada>with Generic_Heapsort; |
|||
with Ada.Text_Io; use Ada.Text_Io; |
|||
procedure Test_Generic_Heapsort is |
|||
type Days is (Sun, Mon, Tue, Wed, Thu, Fri, Sat); |
|||
type Days_Col is array(Days range <>) of Natural; |
|||
procedure Sort is new Generic_Heapsort(Natural, Days, Days_Col); |
|||
Week : Days_Col := (5, 2, 7, 3, 4, 9, 1); |
|||
begin |
|||
for I in Week'range loop |
|||
Put(Days'Image(I) & ":" & Natural'Image(Week(I)) & " "); |
|||
end loop; |
|||
New_Line; |
|||
Sort(Week); |
|||
for I in Week'range loop |
|||
Put(Days'Image(I) & ":" & Natural'Image(Week(I))& " "); |
|||
end loop; |
|||
New_Line; |
|||
end Test_Generic_Heapsort;</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 20:31, 19 July 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
Heap sort | Merge sort | Patience sort | Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort
This page uses content from Wikipedia. The original article was at Sorting algorithms/Heapsort. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |
Heapsort is an in-place sorting algorithm with worst case and average complexity of O(n logn). The basic idea is to turn the array into a binary heap structure, which has the property that it allows efficient retrieval and removal of the maximal element. We repeatedly "remove" the maximal element from the heap, thus building the sorted list from back to front. Heapsort requires random access, so can only be used on an array-like data structure.
Pseudocode:
function heapSort(a, count) is input: an unordered array a of length count (first place a in max-heap order) heapify(a, count) end := count - 1 while end > 0 do (swap the root(maximum value) of the heap with the last element of the heap) swap(a[end], a[0]) (put the heap back in max-heap order) siftDown(a, 0, end-1) (decrement the size of the heap so that the previous max value will stay in its proper place) end := end - 1 function heapify(a,count) is (start is assigned the index in a of the last parent node) start := (count - 2) / 2 while start ≥ 0 do (sift down the node at index start to the proper place such that all nodes below the start index are in heap order) siftDown(a, start, count-1) start := start - 1 (after sifting down the root all nodes/elements are in heap order) function siftDown(a, start, end) is (end represents the limit of how far down the heap to sift) root := start while root * 2 + 1 ≤ end do (While the root has at least one child) child := root * 2 + 1 (root*2+1 points to the left child) (If the child has a sibling and the child's value is less than its sibling's...) if child + 1 ≤ end and a[child] < a[child + 1] then child := child + 1 (... then point to the right child instead) if a[root] < a[child] then (out of max-heap order) swap(a[root], a[child]) root := child (repeat to continue sifting down the child now) else return
Write a function to sort a collection of integers using heapsort.
Ada
This implementation is a generic heapsort for unconstrained arrays. <lang Ada>generic
type Element_Type is private; type Index_Type is (<>); type Collection is array(Index_Type range <>) of Element_Type; with function "<" (Left, right : element_type) return boolean is <>;
procedure Generic_Heapsort(Item : in out Collection);</lang>
<lang Ada>procedure Generic_Heapsort(Item : in out Collection) is
procedure Swap(Left : in out Element_Type; Right : in out Element_Type) is Temp : Element_Type := Left; begin Left := Right; Right := Temp; end Swap; procedure Sift_Down(Item : in out Collection) is Root : Integer := Index_Type'Pos(Item'First); Child : Integer := Index_Type'Pos(Item'Last); Last : Integer := Index_Type'Pos(Item'Last); begin while Root * 2 + 1 <= Last loop Child := Root * 2 + 1; if Child + 1 <= Last and then Item(index_Type'Val(Child)) < Item(Index_Type'Val(Child + 1)) then Child := Child + 1; end if; if Item(Index_Type'Val(Root)) < Item(Index_Type'Val(Child)) then Swap(Item(Index_Type'Val(Root)), Item(Index_Type'Val(Child))); Root := Child; else exit; end if; end loop; end Sift_Down; procedure Heapify(Item : in out Collection) is First_Pos : Integer := Index_Type'Pos(Index_Type'First); Last_Pos : Integer := Index_Type'Pos(Index_type'Last); Start : Index_type := Index_Type'Val((Last_Pos - First_Pos + 1) / 2); begin loop Sift_Down(Item(Start..Item'Last)); if Start > Index_Type'First then Start := Index_Type'Pred(Start); else exit; end if; end loop; end Heapify; Last_Index : Index_Type := Index_Type'Last;
begin
Heapify(Item); while Last_Index > Index_Type'First loop Swap(Item(Last_Index), Item(Item'First)); Last_Index := Index_Type'Pred(Last_Index); Sift_Down(Item(Item'First..Last_Index)); end loop;
end Generic_Heapsort;</lang> Demo code: <lang Ada>with Generic_Heapsort; with Ada.Text_Io; use Ada.Text_Io;
procedure Test_Generic_Heapsort is
type Days is (Sun, Mon, Tue, Wed, Thu, Fri, Sat); type Days_Col is array(Days range <>) of Natural; procedure Sort is new Generic_Heapsort(Natural, Days, Days_Col); Week : Days_Col := (5, 2, 7, 3, 4, 9, 1);
begin
for I in Week'range loop Put(Days'Image(I) & ":" & Natural'Image(Week(I)) & " "); end loop; New_Line; Sort(Week); for I in Week'range loop Put(Days'Image(I) & ":" & Natural'Image(Week(I))& " "); end loop; New_Line;
end Test_Generic_Heapsort;</lang>
Python
<lang python>def heapsort(lst):
Heapsort. Note: this function sorts in-place (it mutates the list).
# in pseudo-code, heapify only called once, so inline it here for start in range((len(lst)-2)/2, -1, -1): siftdown(lst, start, len(lst)-1)
for end in range(len(lst)-1, 0, -1): lst[end], lst[0] = lst[0], lst[end] siftdown(lst, 0, end - 1) return lst
def siftdown(lst, start, end):
root = start while True: child = root * 2 + 1 if child > end: break if child + 1 <= end and lst[child] < lst[child + 1]: child += 1 if lst[root] < lst[child]: lst[root], lst[child] = lst[child], lst[root] root = child else: break</lang>
Testing:
>>> ary = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0] >>> heapsort(ary) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Ruby
<lang ruby>class Array
def heapsort self.dup.heapsort! end
def heapsort! # in pseudo-code, heapify only called once, so inline it here ((length - 2) / 2).downto(0) {|start| siftdown(start, length - 1)}
# "end" is a ruby keyword (length - 1).downto(1) do |end_| self[end_], self[0] = self[0], self[end_] siftdown(0, end_ - 1) end self end
def siftdown(start, end_) root = start loop do child = root * 2 + 1 break if child > end_ if child + 1 <= end_ and self[child] < self[child + 1] child += 1 end if self[root] < self[child] self[root], self[child] = self[child], self[root] root = child else break end end end
end</lang> Testing:
irb(main):035:0> ary = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0] => [7, 6, 5, 9, 8, 4, 3, 1, 2, 0] irb(main):036:0> ary.heapsort => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Tcl
Based on the algorithm from Wikipedia:
<lang tcl>package require Tcl 8.5
proc heapsort {list {count ""}} {
if {$count eq ""} {
set count [llength $list]
} for {set i [expr {$count/2 - 1}]} {$i >= 0} {incr i -1} {
siftDown list $i [expr {$count - 1}]
} for {set i [expr {$count - 1}]} {$i > 0} {} {
swap list $i 0 incr i -1 siftDown list 0 $i
} return $list
} proc siftDown {varName i j} {
upvar 1 $varName a while true {
set child [expr {$i*2 + 1}] if {$child > $j} { break } if {$child+1 <= $j && [lindex $a $child] < [lindex $a $child+1]} { incr child } if {[lindex $a $i] >= [lindex $a $child]} { break } swap a $i $child set i $child
}
} proc swap {varName x y} {
upvar 1 $varName a set tmp [lindex $a $x] lset a $x [lindex $a $y] lset a $y $tmp
}</lang> Demo code: <lang tcl>puts [heapsort {1 5 3 7 9 2 8 4 6 0}]</lang> Output:
0 1 2 3 4 5 6 7 8 9