Sorting algorithms/Heapsort: Difference between revisions

m
<lang> needs a language
(CoffeeScript)
m (<lang> needs a language)
Line 78:
}
}</lang>
 
=={{header|Ada}}==
This implementation is a generic heapsort for unconstrained arrays.
Line 86 ⟶ 87:
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
Line 157:
New_Line;
end Test_Generic_Heapsort;</lang>
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>heapSort(a) {
Line 192 ⟶ 193:
ListVars
MsgBox</lang>
 
=={{header|BCPL}}==
<lang BCPL>// This can be run using Cintcode BCPL freely available from www.cl.cam.ac.uk/users/mr10.
Line 234 ⟶ 236:
}
newline()
}</lang>
}
</lang>
 
=={{header|C}}==
Line 320 ⟶ 321:
return 0;
}</lang>
 
If you want to be slightly more verbose
<lang cpp>#include <iostream>
Line 334:
 
=={{header|C sharp|C#}}==
 
<lang csharp>using System;
using System.Collections.Generic;
Line 413 ⟶ 412:
 
=={{header|Clojure}}==
<lang lisp>(defn- swap [a i j]
(defn- swap [a i j]
(assoc a i (nth a j) j (nth a i)))
Line 443 ⟶ 441:
</lang>
Example usage:
<lang lisp>user> (heapsort [1 2 4 6 2 3 6])
user> (heapsort [1 2 4 6 2 3 6])
[1 2 2 3 4 6 6]
user> (heapsort [1 2 4 6 2 3 6] >)
[6 6 4 3 2 2 1]
user> (heapsort (list 1 2 4 6 2 3 6))
[1 2 2 3 4 6 6]</lang>
</lang>
 
=={{header|CoffeeScript}}==
<lang coffeescript># Do an in-place heap sort.
# Do an in-place heap sort.
heap_sort = (arr) ->
put_array_in_heap_order(arr)
Line 486 ⟶ 481:
arr = [12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8]
heap_sort arr
console.log arr</lang>
{{out}}
</lang>
<pre>
output
<lang>
> coffee heap.coffee
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
</langpre>
 
 
=={{header|Common Lisp}}==
 
<lang lisp>(defun make-heap (&optional (length 7))
(make-array length :adjustable t :fill-pointer 0))
Line 570 ⟶ 562:
(map nil #'(lambda (e) (heap-insert h e predicate)) sequence)
(map-into sequence #'(lambda () (heap-delete-min h predicate)))))</lang>
 
Example usage:
 
<pre>(heapsort (vector 1 9 2 8 3 7 4 6 5) '<) ; #(1 2 3 4 5 6 7 8 9)
(heapsort (list 9 8 1 2 7 6 3 4 5) '<) ; (1 2 3 4 5 6 7 8 9)</pre>
Line 611 ⟶ 601:
 
=={{header|E}}==
 
{{trans|Python}}
 
<lang e>def heapsort := {
def cswap(c, a, b) {
Line 674 ⟶ 662:
for term = n - 1 downto 1 do
swap a term 0
sift cmp a 0 term</lang>
 
</lang>
=={{header|Forth}}==
This program assumes that return addresses simply reside as a single cell on the Return Stack. Most Forth compilers fulfill this requirement.
Line 732 ⟶ 720:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
 
Translation of the pseudocode
<lang fortran>program Heapsort_Demo
Line 799 ⟶ 786:
 
end program Heapsort_Demo</lang>
 
=={{header|Go}}==
 
Here's an ingenious solution that makes use of the heap module. Although the heap module usually implements an independent heap with push/pop operations, we use a helper type where the "pop" operation does not actually change the size of the underlying container, but changes a "heap length" variable indicating the length of the prefix of the underlying container that is considered "the heap".
 
Since we want to implement a generic algorithm, we accept an argument of type <code>sort.Interface</code>, and thus do not have access to the actual elements of the container we're sorting. We can only swap elements. This causes a problem for us when implementing the <code>Pop</code> method, as we can't actually return an element. The ingenious step is realizing that <code>heap.Pop()</code> must move the value to pop to the "end" of the heap area, because its interface only has access to a "Swap" function, and a "Pop" function that pops from the end. (It does not have the ability to pop a value at the beginning.) This is perfect because we precisely want to move the thing popped to the end and shrink the "heap area" by 1. Our "Pop" function returns nothing since we can't get the value, but don't actually need it. (We only need the swapping that it does for us.)
 
<lang go>package main
 
Line 843 ⟶ 829:
fmt.Println("after: ", a)
}</lang>
{{out}}
 
Output:
<pre>
before: [170 45 75 -90 -802 24 2 66]
after: [-802 -90 2 24 45 66 75 170]
</pre>
 
If you want to implement it manually:
<lang go>package main
Line 917 ⟶ 901:
list
}</lang>
 
Test:
<lang groovy>println (heapSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4]))
println (heapSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]))</lang>
{{out}}
 
Output:
<pre>.......................................................................[4, 12, 14, 23, 24, 24, 31, 35, 38, 46, 51, 57, 57, 58, 76, 78, 89, 92, 95, 97, 99]
..........................................................................................[0, 1, 4, 5, 7, 8, 12, 14, 18, 20, 31, 33, 44, 62, 70, 73, 75, 76, 78, 81, 82, 84, 88]</pre>
Line 928 ⟶ 910:
=={{header|Haskell}}==
Using package [http://hackage.haskell.org/package/fgl fgl] from HackageDB
 
<lang haskell>import Data.Graph.Inductive.Internal.Heap(
Heap(..),insert,findMin,deleteMin)
Line 949 ⟶ 930:
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure main() #: demonstrate various ways to sort a list and string
<lang Icon>
procedure main() #: demonstrate various ways to sort a list and string
demosort(heapsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 987 ⟶ 967:
return X
end</lang>
 
Algorithm notes:
* This is a fairly straight forward implementation of the pseudo-code with 'heapify' coded in-line.
Line 995 ⟶ 974:
 
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]]. The full demosort exercises the named sort of a list with op = "numeric", "string", ">>" (lexically gt, descending),">" (numerically gt, descending), a custom comparator, and also a string.
{{out|Abbreviated sample output}}
 
Abbreviated sample output:<pre>Sorting Demo using procedure heapsort
on list : [ 3 14 1 5 9 2 6 3 ]
with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms)
Line 1,164 ⟶ 1,143:
next i
print
end sub</lang>
</lang>
 
=={{header|M4}}==
Line 1,245 ⟶ 1,223:
]
]</lang>
{{out}}
 
<pre>heapSort@{2,3,1,5,7,6}
{1,2,3,5,6,7}</pre>
Line 1,251 ⟶ 1,229:
=={{header|MATLAB}} / {{header|Octave}}==
This function definition is an almost exact translation of the pseudo-code into MATLAB, but I have chosen to make the heapify function inline because it is only called once in the pseudo-code. Also, MATLAB uses 1 based array indecies, therefore all of the pseudo-code has been translated to reflect that difference.
 
<lang MATLAB>function list = heapSort(list)
 
Line 1,292 ⟶ 1,269:
end</lang>
 
Sample Usage:
<lang MATLAB>>> heapSort([4 3 1 5 6 2])
Line 1,383 ⟶ 1,359:
end root
 
return a</lang>
{{out}}
</lang>
;Output
<pre>
UK London
Line 1,408 ⟶ 1,383:
=={{header|Objeck}}==
{{trans|Java}}
<lang objeck>bundle Default {
bundle Default {
class HeapSort {
function : Main(args : String[]) ~ Nil {
Line 1,461 ⟶ 1,435:
}
}
}</lang>
}
</lang>
 
=={{header|OCaml}}==
Line 1,499 ⟶ 1,472:
Array.iter print_char b;;
print_newline ();;</lang>
{{out}}
Output:
<pre>
1 1 2 3 3 4 5 5 5 6 8 9 23 27 33 62 64 83 84 93 95 97
Line 1,644 ⟶ 1,617:
writeln;
end.</lang>
{{out}}
Output:
<pre>
The data before sorting:
Line 1,725 ⟶ 1,698:
say 'Input = ' ~ @data;
@data.&heap_sort;
say 'Output = ' ~ @data;</lang>
{{out}}
</lang>
<pre>
 
Output:<pre>Input = 6 7 2 1 8 9 5 3 4
Output = 1 2 3 4 5 6 7 8 9
</pre>
Line 1,752 ⟶ 1,725:
(xchg (nth A Root) (nth A Child))
(setq Root Child) ) ) )</lang>
{{out}}
Output:
<pre>: (heapSort (make (do 9 (link (rand 1 999)))))
-> (1 167 183 282 524 556 638 891 902)</pre>
Line 1,825 ⟶ 1,798:
 
=={{header|REXX}}==
<lang rexx>/*REXX program sorts an array using the heapsort method. */
<lang rexx>
/*REXX program sorts an array using the heapsort method. */
 
call gen@ /*generate array elements. */
Line 1,908 ⟶ 1,880:
 
say copies('─',80) /*show a seperator line. */
return</lang>
{{out}}
</lang>
Output:
<pre style="height:30ex;overflow:scroll">
element 1 before sort: ---letters of the modern Greek Alphabet---
Line 2,011 ⟶ 1,982:
=={{header|Scala}}==
{{works with|Scala|2.8}}
This code is not written for maximum performance, though, of course, it preserves the O(n log n) characteristic of heap sort.
 
This code is not written for maximum performance, though, of course, it preserves the
O(n log n) characteristic of heap sort.
 
<lang scala>def heapSort[T](a: Array[T])(implicit ord: Ordering[T]) {
import scala.annotation.tailrec // Ensure functions are tail-recursive
Line 2,113 ⟶ 2,081:
(define uriah (list->vector '(3 5 7 9 0 8 1 4 2 6)))
(heapsort uriah)
uriah</lang>
{{out}}
</lang>
<langpre>done
Output:
#(0 1 2 3 4 5 6 7 8 9)</langpre>
<lang>done
#(0 1 2 3 4 5 6 7 8 9)</lang>
 
=={{header|Seed7}}==
Line 2,159 ⟶ 2,126:
until n <= 1;
end func;</lang>
 
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#heapSort]
 
=={{header|Tcl}}==
Based on the algorithm from Wikipedia:<br>
{{works with|Tcl|8.5}}
<lang tcl>package require Tcl 8.5
Line 2,206 ⟶ 2,172:
Demo code:
<lang tcl>puts [heapsort {1 5 3 7 9 2 8 4 6 0}]</lang>
{{out}}
Output:
<pre>0 1 2 3 4 5 6 7 8 9</pre>
 
Anonymous user