Anonymous user
Sorting algorithms/Insertion sort: Difference between revisions
Sorting algorithms/Insertion sort (view source)
Revision as of 18:49, 20 November 2009
, 14 years agoFixed lang tags.
m (Fixed lang tags.) |
|||
Line 44:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol68>MODE DATA = REF CHAR;▼
▲MODE DATA = REF CHAR;
PROC in place insertion sort = (REF[]DATA item)VOID:
Line 69 ⟶ 68:
in place insertion sort(ref data);
FOR i TO UPB ref data DO print(ref data[i]) OD; print(new line);
print((data))</lang>
Output:
big fjords vex quick waltz nymph</lang>▼
▲ abcdefghiijklmnopqrstuvwxyz
▲big fjords vex quick waltz nymph
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276481.html#276481 forum]
Line 201 ⟶ 197:
=={{header|Clojure}}==
Translated from the Haskell example:
<lang lisp>(defn in-sort! [data]
(letfn [(insert ([raw x](insert [] raw x))
([sorted [y & raw] x]
Line 210 ⟶ 205:
(reduce insert [] data)))
;Usage:(in-sort! [6,8,5,9,3,2,1,4,7])
;Returns: [1 2 3 4 5 6 7 8 9]</lang>
</lang>▼
=={{header|Common Lisp}}==
Line 275 ⟶ 269:
=={{header|Erlang}}==
<lang Erlang>-module(sort).
-export([insertion/1]).
Line 283 ⟶ 276:
insert(X,[]) -> [X];
insert(X,L=[H|_]) when X =< H -> [X|L];
insert(X,[H|T]) -> [H|insert(X, T)].</lang>
And the calls:
<lang erlang>1> c(sort).
{ok,sort}
2> sort:insertion([5,3,9,4,1,6,8,2,7]).
[1,2,3,4,5,6,7,8,9]</lang>
=={{header|Forth}}==
<
dup @ >r ( r: v ) \ v = a[i]
begin
Line 312 ⟶ 302:
create test 7 , 3 , 0 , 2 , 9 , 1 , 6 , 8 , 4 , 5 ,
test 10 sort
test 10 cells dump</lang>
=={{header|Fortran}}==
Line 352 ⟶ 341:
=={{header|J}}==
Solution inspired by the Common LISP solution:
▲ isort=:((>: # ]) , [ , < #])/
Example of use:
<lang J> isort 32 4 1 34 95 3 2 120 _38
_38 1 2
=={{header|Java}}==
Line 424 ⟶ 409:
=={{header|PL/I}}==
DCL (A(*)) FIXED BIN(31),
▲ INSSORT: PROCEDURE (A,N);
DCL
DO
DO WHILE (J
▲ END INSSORT;
=={{header|Prolog}}==
Line 490 ⟶ 473:
=={{header|R}}==
Direct translation of pseudocode.
<lang r>insertionsort <- function(x)
{
for(i in 2:(length(x)))
Line 506 ⟶ 488:
x
}
insertionsort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</lang>
=={{header|Ruby}}==
Line 568 ⟶ 549:
arr[j] := help;
end for;
end func;</lang>
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#insertionSort]
Line 604 ⟶ 584:
=={{header|UnixPipes}}==
<lang bash>selectionsort() {▼
▲selectionsort() {
read a
test -n "$a" && ( selectionsort | sort -nm <(echo $a) -)
▲}</lang>
<lang bash>cat to.sort | selectionsort</lang>▼
▲cat to.sort | selectionsort
=={{header|Ursala}}==
<lang Ursala>#import nat
insort = ~&i&& @hNCtX ~&r->lx ^\~&rt nleq-~rlrSPrhlPrSCPTlrShlPNCTPQ@rhPlD</lang>
test program:
<lang Ursala>#cast %nL
example = insort <45,82,69,82,104,58,88,112,89,74></lang>
output:
<pre>
|