Sorting algorithms/Selection sort: Difference between revisions
Content deleted Content added
Added AppleScript. |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 28: | Line 28: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F selection_sort(&lst) |
||
L(e) lst |
L(e) lst |
||
V mn = min(L.index .< lst.len, key' x -> @lst[x]) |
V mn = min(L.index .< lst.len, key' x -> @lst[x]) |
||
Line 35: | Line 35: | ||
V arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0] |
V arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0] |
||
selection_sort(&arr) |
selection_sort(&arr) |
||
print(arr)</ |
print(arr)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 45: | Line 45: | ||
{{trans|PL/I}} |
{{trans|PL/I}} |
||
The program uses ASM structured macros and two ASSIST macros to keep the code as short as possible. |
The program uses ASM structured macros and two ASSIST macros to keep the code as short as possible. |
||
< |
<syntaxhighlight lang="360asm">* Selection sort 26/06/2016 |
||
SELECSRT CSECT |
SELECSRT CSECT |
||
USING SELECSRT,R13 base register |
USING SELECSRT,R13 base register |
||
Line 105: | Line 105: | ||
RK EQU 8 k |
RK EQU 8 k |
||
RT EQU 9 temp |
RT EQU 9 temp |
||
END SELECSRT</ |
END SELECSRT</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 113: | Line 113: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program selectionSort64.s */ |
/* program selectionSort64.s */ |
||
Line 278: | Line 278: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size) |
||
INT i |
INT i |
||
Line 336: | Line 336: | ||
Test(c,8) |
Test(c,8) |
||
Test(d,12) |
Test(d,12) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Selection_sort.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Selection_sort.png Screenshot from Atari 8-bit computer] |
||
Line 362: | Line 362: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">function selectionSort(input: Array):Array { |
||
//find the i'th element |
//find the i'th element |
||
for (var i:uint = 0; i < input.length; i++) { |
for (var i:uint = 0; i < input.length; i++) { |
||
Line 379: | Line 379: | ||
} |
} |
||
return input; |
return input; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Test_Selection_Sort is |
procedure Test_Selection_Sort is |
||
Line 412: | Line 412: | ||
Put (Integer'Image (A (I)) & " "); |
Put (Integer'Image (A (I)) & " "); |
||
end loop; |
end loop; |
||
end Test_Selection_Sort;</ |
end Test_Selection_Sort;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 424: | Line 424: | ||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
{{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}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
< |
<syntaxhighlight lang="algol68">MODE DATA = REF CHAR; |
||
PROC in place selection sort = (REF[]DATA a)VOID: |
PROC in place selection sort = (REF[]DATA a)VOID: |
||
Line 449: | Line 449: | ||
in place selection sort(ref data); |
in place selection sort(ref data); |
||
FOR i TO UPB ref data DO print(ref data[i]) OD; print(new line); |
FOR i TO UPB ref data DO print(ref data[i]) OD; print(new line); |
||
print((data))</ |
print((data))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 457: | Line 457: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">on selectionSort(theList, l, r) -- Sort items l thru r of theList in place. |
||
set listLength to (count theList) |
set listLength to (count theList) |
||
if (listLength < 2) then return |
if (listLength < 2) then return |
||
Line 494: | Line 494: | ||
end demo |
end demo |
||
demo()</ |
demo()</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">{31, 71, 151, 177, 558, 627, 712, 906, 945, 988}</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program selectionSort.s */ |
/* program selectionSort.s */ |
||
Line 730: | Line 730: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">selectionSort: function [items][ |
||
sorted: new [] |
sorted: new [] |
||
tmp: new items |
tmp: new items |
||
Line 745: | Line 745: | ||
] |
] |
||
print selectionSort [3 1 2 8 5 7 9 4 6]</ |
print selectionSort [3 1 2 8 5 7 9 4 6]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 753: | Line 753: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
ahk forum: [http://www.autohotkey.com/forum/topic44657-105.html discussion] |
ahk forum: [http://www.autohotkey.com/forum/topic44657-105.html discussion] |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % SelecSort("") |
||
MsgBox % SelecSort("xxx") |
MsgBox % SelecSort("xxx") |
||
MsgBox % SelecSort("3,2,1") |
MsgBox % SelecSort("3,2,1") |
||
Line 773: | Line 773: | ||
sorted .= "," . a%A_Index% |
sorted .= "," . a%A_Index% |
||
Return SubStr(sorted,2) ; drop leading comma |
Return SubStr(sorted,2) ; drop leading comma |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">function getminindex(gl, gi, gs) |
||
{ |
{ |
||
min = gl[gi] |
min = gl[gi] |
||
Line 803: | Line 803: | ||
print line[i] |
print line[i] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic">DEF PROC_SelectionSort(Size%) |
||
FOR I% = 1 TO Size%-1 |
FOR I% = 1 TO Size%-1 |
||
lowest% = I% |
lowest% = I% |
||
Line 814: | Line 814: | ||
IF I%<>lowest% SWAP data%(I%),data%(lowest%) |
IF I%<>lowest% SWAP data%(I%),data%(lowest%) |
||
NEXT I% |
NEXT I% |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|GWBASIC}}=== |
==={{header|GWBASIC}}=== |
||
Works with: QBASIC, QuickBASIC, VB-DOS |
Works with: QBASIC, QuickBASIC, VB-DOS |
||
<syntaxhighlight lang="gwbasic"> |
|||
<lang GWBASIC> |
|||
10 'SAVE"SELSORT",A |
10 'SAVE"SELSORT",A |
||
20 ' Selection Sort Algorithm |
20 ' Selection Sort Algorithm |
||
Line 866: | Line 866: | ||
450 PRINT "End of program" |
450 PRINT "End of program" |
||
460 END |
460 END |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let selectionsort(A, len) be if len > 1 |
let selectionsort(A, len) be if len > 1 |
||
Line 892: | Line 892: | ||
selectionsort(array, length) |
selectionsort(array, length) |
||
writes("Output: ") ; writearray(array, length) ; wrch('*N') |
writes("Output: ") ; writearray(array, length) ; wrch('*N') |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Input: 52 -5 -20 199 65 -3 190 25 9999 -5342 |
<pre>Input: 52 -5 -20 199 65 -3 190 25 9999 -5342 |
||
Line 899: | Line 899: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
void selection_sort (int *a, int n) { |
void selection_sort (int *a, int n) { |
||
Line 926: | Line 926: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 936: | Line 936: | ||
This is a generic implementation that works with any type that implements the IComparable interface |
This is a generic implementation that works with any type that implements the IComparable interface |
||
< |
<syntaxhighlight lang="csharp">class SelectionSort<T> where T : IComparable { |
||
public T[] Sort(T[] list) { |
public T[] Sort(T[] list) { |
||
int k; |
int k; |
||
Line 955: | Line 955: | ||
return list; |
return list; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Example of usage: |
Example of usage: |
||
< |
<syntaxhighlight lang="csharp">String[] str = { "this", "is", "a", "test", "of", "generic", "selection", "sort" }; |
||
SelectionSort<String> mySort = new SelectionSort<string>(); |
SelectionSort<String> mySort = new SelectionSort<string>(); |
||
Line 966: | Line 966: | ||
for (int i = 0; i < result.Length; i++) { |
for (int i = 0; i < result.Length; i++) { |
||
Console.WriteLine(result[i]); |
Console.WriteLine(result[i]); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 981: | Line 981: | ||
Uses C++11. Compile with |
Uses C++11. Compile with |
||
g++ -std=c++11 selection.cpp |
g++ -std=c++11 selection.cpp |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iterator> |
#include <iterator> |
||
#include <iostream> |
#include <iostream> |
||
Line 997: | Line 997: | ||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
||
std::cout << "\n"; |
std::cout << "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,006: | Line 1,006: | ||
This is an implementation that mutates a Java arraylist in place. |
This is an implementation that mutates a Java arraylist in place. |
||
< |
<syntaxhighlight lang="lisp">(import 'java.util.ArrayList) |
||
(defn arr-swap! [#^ArrayList arr i j] |
(defn arr-swap! [#^ArrayList arr i j] |
||
Line 1,027: | Line 1,027: | ||
(doseq [start-i (range (dec n))] |
(doseq [start-i (range (dec n))] |
||
(move-min! start-i)) |
(move-min! start-i)) |
||
arr))))</ |
arr))))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> PERFORM E-SELECTION VARYING WB-IX-1 FROM 1 BY 1 |
||
UNTIL WB-IX-1 = WC-SIZE. |
UNTIL WB-IX-1 = WC-SIZE. |
||
Line 1,057: | Line 1,057: | ||
F-999. |
F-999. |
||
EXIT.</ |
EXIT.</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun selection-sort-vector (array predicate) |
||
(do ((length (length array)) |
(do ((length (length array)) |
||
(i 0 (1+ i))) |
(i 0 (1+ i))) |
||
Line 1,097: | Line 1,097: | ||
(etypecase sequence |
(etypecase sequence |
||
(list (selection-sort-list sequence predicate)) |
(list (selection-sort-list sequence predicate)) |
||
(vector (selection-sort-vector sequence predicate))))</ |
(vector (selection-sort-vector sequence predicate))))</syntaxhighlight> |
||
Example use: |
Example use: |
||
Line 1,109: | Line 1,109: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
This sorts the array in-place. |
This sorts the array in-place. |
||
< |
<syntaxhighlight lang="crystal">def selectionSort(array : Array) |
||
(0...array.size-1).each do |i| |
(0...array.size-1).each do |i| |
||
nextMinIndex = i |
nextMinIndex = i |
||
Line 1,121: | Line 1,121: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
The actual function is very short. |
The actual function is very short. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.array, std.traits; |
||
enum AreSortableArrayItems(T) = isMutable!T && |
enum AreSortableArrayItems(T) = isMutable!T && |
||
Line 1,185: | Line 1,185: | ||
a.selectionSort; |
a.selectionSort; |
||
a.writeln; |
a.writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, 9, 9, 9]</pre> |
<pre>[1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, 9, 9, 9]</pre> |
||
Line 1,192: | Line 1,192: | ||
{{trans | Java}} |
{{trans | Java}} |
||
< |
<syntaxhighlight lang="dart"> |
||
void main() { |
void main() { |
||
List<int> a = selectionSort([1100, 2, 56, 200, -52, 3, 99, 33, 177, -199]); |
List<int> a = selectionSort([1100, 2, 56, 200, -52, 3, 99, 33, 177, -199]); |
||
Line 1,214: | Line 1,214: | ||
return array; |
return array; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,226: | Line 1,226: | ||
Static array is an arbitrary-based array of fixed length |
Static array is an arbitrary-based array of fixed length |
||
< |
<syntaxhighlight lang="delphi">program TestSelectionSort; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 1,274: | Line 1,274: | ||
Writeln; |
Writeln; |
||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,283: | Line 1,283: | ||
===String sort=== |
===String sort=== |
||
// string is 1-based variable-length array of Char |
// string is 1-based variable-length array of Char |
||
< |
<syntaxhighlight lang="delphi">procedure SelectionSort(var S: string); |
||
var |
var |
||
Lowest: Char; |
Lowest: Char; |
||
Line 1,298: | Line 1,298: | ||
S[I]:= Lowest; |
S[I]:= Lowest; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
<pre> |
<pre> |
||
// in : S = 'the quick brown fox jumps over the lazy dog' |
// in : S = 'the quick brown fox jumps over the lazy dog' |
||
Line 1,306: | Line 1,306: | ||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang="e">def selectionSort := { |
||
def cswap(c, a, b) { |
def cswap(c, a, b) { |
||
def t := c[a] |
def t := c[a] |
||
Line 1,333: | Line 1,333: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>subr sort |
<syntaxhighlight lang="text">subr sort |
||
for i = 0 to len data[] - 2 |
for i = 0 to len data[] - 2 |
||
min_pos = i |
min_pos = i |
||
Line 1,350: | Line 1,350: | ||
data[] = [ 29 4 72 44 55 26 27 77 92 5 ] |
data[] = [ 29 4 72 44 55 26 27 77 92 5 ] |
||
call sort |
call sort |
||
print data[]</ |
print data[]</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
===List sort=== |
===List sort=== |
||
< |
<syntaxhighlight lang="scheme"> |
||
;; recursive version (adapted from Racket) |
;; recursive version (adapted from Racket) |
||
(lib 'list) ;; list-delete |
(lib 'list) ;; list-delete |
||
Line 1,374: | Line 1,374: | ||
→ (0 1 2 3 4 5 6 7 8 9 10 11 12) |
→ (0 1 2 3 4 5 6 7 8 9 10 11 12) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Array sort=== |
===Array sort=== |
||
< |
<syntaxhighlight lang="scheme"> |
||
;; sort an array in place |
;; sort an array in place |
||
(define (sel-sort a (amin) (imin)) |
(define (sel-sort a (amin) (imin)) |
||
Line 1,390: | Line 1,390: | ||
(sel-sort a) |
(sel-sort a) |
||
→ #( 2 3 4 5 6 8 9) |
→ #( 2 3 4 5 6 8 9) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
SELECTION_SORT [G -> COMPARABLE] |
SELECTION_SORT [G -> COMPARABLE] |
||
Line 1,478: | Line 1,478: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test: |
Test: |
||
< |
<syntaxhighlight lang="eiffel"> |
||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 1,513: | Line 1,513: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,522: | Line 1,522: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
import system'routines; |
import system'routines; |
||
Line 1,554: | Line 1,554: | ||
console.printLine("before:",list.asEnumerable()); |
console.printLine("before:",list.asEnumerable()); |
||
console.printLine("after:",list.selectionSort().asEnumerable()) |
console.printLine("after:",list.selectionSort().asEnumerable()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,562: | Line 1,562: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Sort do |
||
def selection_sort(list) when is_list(list), do: selection_sort(list, []) |
def selection_sort(list) when is_list(list), do: selection_sort(list, []) |
||
Line 1,570: | Line 1,570: | ||
selection_sort(List.delete(list, max), [max | sorted]) |
selection_sort(List.delete(list, max), [max | sorted]) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
Example: |
Example: |
||
Line 1,579: | Line 1,579: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module(solution). |
-module(solution). |
||
-import(lists,[delete/2,max/1]). |
-import(lists,[delete/2,max/1]). |
||
Line 1,596: | Line 1,596: | ||
Ans=selection_sort([1,5,7,8,4,10],[]), |
Ans=selection_sort([1,5,7,8,4,10],[]), |
||
print_array(Ans). |
print_array(Ans). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">function selection_sort(sequence s) |
||
object tmp |
object tmp |
||
integer m |
integer m |
||
Line 1,622: | Line 1,622: | ||
pretty_print(1,s,{2}) |
pretty_print(1,s,{2}) |
||
puts(1,"\nAfter: ") |
puts(1,"\nAfter: ") |
||
pretty_print(1,selection_sort(s),{2})</ |
pretty_print(1,selection_sort(s),{2})</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,659: | Line 1,659: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let rec ssort = function |
let rec ssort = function |
||
[] -> [] |
[] -> [] |
||
Line 1,669: | Line 1,669: | ||
(x, []) xs |
(x, []) xs |
||
in min::ssort rest |
in min::ssort rest |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: kernel math sequences sequences.extras ; |
||
: select ( m n seq -- ) |
: select ( m n seq -- ) |
||
Line 1,679: | Line 1,679: | ||
: selection-sort! ( seq -- seq' ) |
: selection-sort! ( seq -- seq' ) |
||
[ ] [ length dup ] [ ] tri [ select ] 2curry each-integer ;</ |
[ ] [ length dup ] [ ] tri [ select ] 2curry each-integer ;</syntaxhighlight> |
||
Example use |
Example use |
||
< |
<syntaxhighlight lang="factor">IN: scratchpad { 5 -6 3 9 -2 4 -1 -6 5 -5 } selection-sort! |
||
--- Data stack: |
--- Data stack: |
||
{ -6 -6 -5 -2 -1 3 4 5 5 9 }</ |
{ -6 -6 -5 -2 -1 3 4 5 5 9 }</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">defer less? ' < is less? |
||
: least ( start end -- least ) |
: least ( start end -- least ) |
||
Line 1,703: | Line 1,703: | ||
array 10 selection |
array 10 selection |
||
array 10 cells dump</ |
array 10 cells dump</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|95 and later}} |
{{works with|Fortran|95 and later}} |
||
< |
<syntaxhighlight lang="fortran">PROGRAM SELECTION |
||
IMPLICIT NONE |
IMPLICIT NONE |
||
Line 1,733: | Line 1,733: | ||
END SUBROUTINE Selection_sort |
END SUBROUTINE Selection_sort |
||
END PROGRAM SELECTION</ |
END PROGRAM SELECTION</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,741: | Line 1,741: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' version 03-12-2016 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
' for boundry checks on array's compile with: fbc -s console -exx |
' for boundry checks on array's compile with: fbc -s console -exx |
||
Line 1,787: | Line 1,787: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>unsort 1 -7 -5 -4 6 5 -3 4 2 0 3 -6 -2 7 -1 |
<pre>unsort 1 -7 -5 -4 6 5 -3 4 2 0 3 -6 -2 7 -1 |
||
Line 1,793: | Line 1,793: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
< |
<syntaxhighlight lang="gambas"> |
||
siLow As Short = -99 'Set the lowest value number to create |
siLow As Short = -99 'Set the lowest value number to create |
||
siHigh As Short = 99 'Set the highest value number to create |
siHigh As Short = 99 'Set the highest value number to create |
||
Line 1,842: | Line 1,842: | ||
Return siList |
Return siList |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,851: | Line 1,851: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">SelectionSort := function(v) |
||
local i, j, k, n, m; |
local i, j, k, n, m; |
||
n := Size(v); |
n := Size(v); |
||
Line 1,870: | Line 1,870: | ||
v := List([1 .. 100], n -> Random([1 .. 100])); |
v := List([1 .. 100], n -> Random([1 .. 100])); |
||
SelectionSort(v); |
SelectionSort(v); |
||
v;</ |
v;</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,898: | Line 1,898: | ||
a[i], a[iMin] = aMin, a[i] |
a[i], a[iMin] = aMin, a[i] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
More generic version that sorts anything that implements <code>sort.Interface</code>: |
More generic version that sorts anything that implements <code>sort.Interface</code>: |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,927: | Line 1,927: | ||
a.Swap(i, iMin) |
a.Swap(i, iMin) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (delete) |
||
selSort :: (Ord a) => [a] -> [a] |
selSort :: (Ord a) => [a] -> [a] |
||
selSort [] = [] |
selSort [] = [] |
||
selSort xs = selSort (delete x xs) ++ [x] |
selSort xs = selSort (delete x xs) ++ [x] |
||
where x = maximum xs</ |
where x = maximum xs</syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
< |
<syntaxhighlight lang="haxe">class SelectionSort { |
||
@:generic |
@:generic |
||
public static function sort<T>(arr:Array<T>) { |
public static function sort<T>(arr:Array<T>) { |
||
Line 1,975: | Line 1,975: | ||
Sys.println('Sorted Strings: ' + stringArray); |
Sys.println('Sorted Strings: ' + stringArray); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,988: | Line 1,988: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main() #: demonstrate various ways to sort a list and string |
||
demosort(selectionsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") |
demosort(selectionsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") |
||
end |
end |
||
Line 2,004: | Line 2,004: | ||
} |
} |
||
return X |
return X |
||
end</ |
end</syntaxhighlight> |
||
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. |
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. |
||
Line 2,017: | Line 2,017: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">List do ( |
||
selectionSortInPlace := method( |
selectionSortInPlace := method( |
||
size repeat(idx, |
size repeat(idx, |
||
Line 2,026: | Line 2,026: | ||
l := list(-1, 4, 2, -9) |
l := list(-1, 4, 2, -9) |
||
l selectionSortInPlace println # ==> list(-9, -1, 2, 4)</ |
l selectionSortInPlace println # ==> list(-9, -1, 2, 4)</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang="is-basic">100 PROGRAM "SelecSrt.bas" |
||
110 RANDOMIZE |
110 RANDOMIZE |
||
120 NUMERIC ARRAY(-5 TO 14) |
120 NUMERIC ARRAY(-5 TO 14) |
||
Line 2,055: | Line 2,055: | ||
340 LET A(INDEX)=A(I):LET A(I)=MN |
340 LET A(INDEX)=A(I):LET A(I)=MN |
||
350 NEXT |
350 NEXT |
||
360 END DEF</ |
360 END DEF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
{{eff note|J|/:~}} |
{{eff note|J|/:~}} |
||
Create the following script and load it to a J session. |
Create the following script and load it to a J session. |
||
< |
<syntaxhighlight lang="j">selectionSort=: verb define |
||
data=. y |
data=. y |
||
for_xyz. y do. |
for_xyz. y do. |
||
Line 2,068: | Line 2,068: | ||
end. |
end. |
||
data |
data |
||
)</ |
)</syntaxhighlight> |
||
In an email discussion, Roger_Hui presented the following tacit code: |
In an email discussion, Roger_Hui presented the following tacit code: |
||
< |
<syntaxhighlight lang="j">ix=: C.~ <@~.@(0, (i. <./)) |
||
ss1=: ({. , $:@}.)@ix^:(*@#)</ |
ss1=: ({. , $:@}.)@ix^:(*@#)</syntaxhighlight> |
||
To validate: |
To validate: |
||
< |
<syntaxhighlight lang="j"> [data=. 6 15 19 12 14 19 0 17 0 14 |
||
6 15 19 12 14 19 0 17 0 14 |
6 15 19 12 14 19 0 17 0 14 |
||
selectionSort data |
selectionSort data |
||
0 0 6 12 14 14 15 17 19 19 |
0 0 6 12 14 14 15 17 19 19 |
||
ss1 data |
ss1 data |
||
0 0 6 12 14 14 15 17 19 19</ |
0 0 6 12 14 14 15 17 19 19</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
This algorithm sorts in place. The call <tt>sort(array)</tt> will rearrange the array and not create a new one. |
This algorithm sorts in place. The call <tt>sort(array)</tt> will rearrange the array and not create a new one. |
||
< |
<syntaxhighlight lang="java">public static void sort(int[] nums){ |
||
for(int currentPlace = 0;currentPlace<nums.length-1;currentPlace++){ |
for(int currentPlace = 0;currentPlace<nums.length-1;currentPlace++){ |
||
int smallest = Integer.MAX_VALUE; |
int smallest = Integer.MAX_VALUE; |
||
Line 2,098: | Line 2,098: | ||
nums[smallestAt] = temp; |
nums[smallestAt] = temp; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
This algorithm sorts array of numbers. |
This algorithm sorts array of numbers. |
||
< |
<syntaxhighlight lang="javascript">function selectionSort(nums) { |
||
var len = nums.length; |
var len = nums.length; |
||
for(var i = 0; i < len; i++) { |
for(var i = 0; i < len; i++) { |
||
Line 2,118: | Line 2,118: | ||
} |
} |
||
return nums; |
return nums; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
The following implementation does not impose any restrictions on the types of entities that may appear in the array to be sorted. That is, the array may include any collection of JSON entities. |
The following implementation does not impose any restrictions on the types of entities that may appear in the array to be sorted. That is, the array may include any collection of JSON entities. |
||
The definition also illustrates the use of an inner function (swap), and the use of jq's reduction operator, <tt>reduce</tt>.< |
The definition also illustrates the use of an inner function (swap), and the use of jq's reduction operator, <tt>reduce</tt>.<syntaxhighlight lang="jq"># Sort any array |
||
def selection_sort: |
def selection_sort: |
||
def swap(i;j): if i == j then . else .[i] as $tmp | .[i] = .[j] | .[j] = $tmp end; |
def swap(i;j): if i == j then . else .[i] as $tmp | .[i] = .[j] | .[j] = $tmp end; |
||
Line 2,140: | Line 2,140: | ||
)) as $ans |
)) as $ans |
||
| swap( $currentPlace; $ans[0] ) |
| swap( $currentPlace; $ans[0] ) |
||
) ;</ |
) ;</syntaxhighlight>Example:<syntaxhighlight lang="jq"> |
||
[1, 3.3, null, 2, null, [1,{"a":1 }] ] | selection_sort |
[1, 3.3, null, 2, null, [1,{"a":1 }] ] | selection_sort |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,163: | Line 2,163: | ||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">function selectionsort!(arr::Vector{<:Real}) |
||
len = length(arr) |
len = length(arr) |
||
if len < 2 return arr end |
if len < 2 return arr end |
||
Line 2,177: | Line 2,177: | ||
v = rand(-10:10, 10) |
v = rand(-10:10, 10) |
||
println("# unordered: $v\n -> ordered: ", selectionsort!(v))</ |
println("# unordered: $v\n -> ordered: ", selectionsort!(v))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,185: | Line 2,185: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="scala">fun <T : Comparable<T>> Array<T>.selection_sort() { |
||
for (i in 0..size - 2) { |
for (i in 0..size - 2) { |
||
var k = i |
var k = i |
||
Line 2,212: | Line 2,212: | ||
c.selection_sort() |
c.selection_sort() |
||
println(c.joinToString()) |
println(c.joinToString()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>-5, -2, 0, 1, 3, 4, 6, 7, 8, 9 |
<pre>-5, -2, 0, 1, 3, 4, 6, 7, 8, 9 |
||
Line 2,219: | Line 2,219: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb"> itemCount = 20 |
||
dim A(itemCount) |
dim A(itemCount) |
||
for i = 1 to itemCount |
for i = 1 to itemCount |
||
Line 2,250: | Line 2,250: | ||
print |
print |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|LSE}}== |
=={{header|LSE}}== |
||
<syntaxhighlight lang="lse"> |
|||
<lang LSE> |
|||
(* |
(* |
||
** Tri par Sélection |
** Tri par Sélection |
||
Line 2,274: | Line 2,274: | ||
BOUCLER |
BOUCLER |
||
FIN PROCEDURE |
FIN PROCEDURE |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function SelectionSort( f ) |
||
for k = 1, #f-1 do |
for k = 1, #f-1 do |
||
local idx = k |
local idx = k |
||
Line 2,296: | Line 2,296: | ||
for i in next, f do |
for i in next, f do |
||
print( f[i] ) |
print( f[i] ) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">arr:= Array([17,3,72,0,36,2,3,8,40,0]): |
||
len := numelems(arr): |
len := numelems(arr): |
||
for i to len-1 do |
for i to len-1 do |
||
Line 2,314: | Line 2,314: | ||
end if: |
end if: |
||
end do: |
end do: |
||
arr;</ |
arr;</syntaxhighlight> |
||
{{Out|Output}} |
{{Out|Output}} |
||
<pre>[0,0,2,3,3,8,17,36,40,72]</pre> |
<pre>[0,0,2,3,3,8,17,36,40,72]</pre> |
||
Line 2,321: | Line 2,321: | ||
Procedural solution with custom min function: |
Procedural solution with custom min function: |
||
< |
<syntaxhighlight lang="mathematica">SelectSort[x_List] := Module[{n = 1, temp, xi = x, j}, |
||
While[n <= Length@x, |
While[n <= Length@x, |
||
temp = xi[[n]]; |
temp = xi[[n]]; |
||
Line 2,332: | Line 2,332: | ||
]; |
]; |
||
xi |
xi |
||
]</ |
]</syntaxhighlight> |
||
Recursive solution using a pre-existing Min[] function: |
Recursive solution using a pre-existing Min[] function: |
||
< |
<syntaxhighlight lang="mathematica">SelectSort2[x_List]:= Flatten[{Min@x, If[Length@x > 1, SelectSort2@Drop[x, First@Position[x, Min@x]], {}] }];</syntaxhighlight> |
||
Validate by testing the ordering of a random number of randomly-sized random lists: |
Validate by testing the ordering of a random number of randomly-sized random lists: |
||
< |
<syntaxhighlight lang="mathematica">{And @@ Table[l = RandomInteger[150, RandomInteger[1000]]; |
||
Through[And[Length@# == Length@SelectSort@# &, OrderedQ@SelectSort@# &]@l], |
Through[And[Length@# == Length@SelectSort@# &, OrderedQ@SelectSort@# &]@l], |
||
{RandomInteger[150]}], |
{RandomInteger[150]}], |
||
Line 2,347: | Line 2,347: | ||
Through[And[Length@# == Length@SelectSort2@# &, OrderedQ@SelectSort2@# &]@l], |
Through[And[Length@# == Length@SelectSort2@# &, OrderedQ@SelectSort2@# &]@l], |
||
{RandomInteger[150]}] |
{RandomInteger[150]}] |
||
]}</ |
]}</syntaxhighlight> |
||
Validation Result: |
Validation Result: |
||
Line 2,354: | Line 2,354: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">function list = selectionSort(list) |
||
listSize = numel(list); |
listSize = numel(list); |
||
Line 2,377: | Line 2,377: | ||
end %for |
end %for |
||
end %selectionSort</ |
end %selectionSort</syntaxhighlight> |
||
Sample Usage: |
Sample Usage: |
||
< |
<syntaxhighlight lang="matlab">>> selectionSort([4 3 1 5 6 2]) |
||
ans = |
ans = |
||
1 2 3 4 5 6</ |
1 2 3 4 5 6</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">selection_sort(v) := block([k, m, n], |
||
n: length(v), |
n: length(v), |
||
for i: 1 thru n do ( |
for i: 1 thru n do ( |
||
Line 2,400: | Line 2,400: | ||
v: makelist(random(199) - 99, i, 1, 10); /* [52, -85, 41, -70, -59, 88, 19, 80, 90, 44] */ |
v: makelist(random(199) - 99, i, 1, 10); /* [52, -85, 41, -70, -59, 88, 19, 80, 90, 44] */ |
||
selection_sort(v)$ |
selection_sort(v)$ |
||
v; /* [-85, -70, -59, 19, 41, 44, 52, 80, 88, 90] */</ |
v; /* [-85, -70, -59, 19, 41, 44, 52, 80, 88, 90] */</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<syntaxhighlight lang="maxscript">fn selectionSort arr = |
||
( |
( |
||
local min = undefined |
local min = undefined |
||
Line 2,422: | Line 2,422: | ||
data = selectionSort #(4, 9, 3, -2, 0, 7, -5, 1, 6, 8) |
data = selectionSort #(4, 9, 3, -2, 0, 7, -5, 1, 6, 8) |
||
print data</ |
print data</syntaxhighlight> |
||
=={{header|N/t/roff}}== |
=={{header|N/t/roff}}== |
||
< |
<syntaxhighlight lang="n/t/roff">.de end |
||
.. |
.. |
||
.de array |
.de array |
||
Line 2,467: | Line 2,467: | ||
.. |
.. |
||
.sort myArray |
.sort myArray |
||
.myArray.dump</ |
.myArray.dump</syntaxhighlight> |
||
===Output=== |
===Output=== |
||
<lang>0 |
<syntaxhighlight lang="text">0 |
||
0 |
0 |
||
0 |
0 |
||
Line 2,488: | Line 2,488: | ||
212 |
212 |
||
483 |
483 |
||
589</ |
589</syntaxhighlight> |
||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="nanoquery">import math |
||
def sort(nums) |
def sort(nums) |
||
Line 2,510: | Line 2,510: | ||
end |
end |
||
return nums |
return nums |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="nemerle">using System; |
||
using System.Console; |
using System.Console; |
||
Line 2,540: | Line 2,540: | ||
foreach (i in arr) Write($"$i "); |
foreach (i in arr) Write($"$i "); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols binary |
options replace format comments java crossref savelog symbols binary |
||
Line 2,599: | Line 2,599: | ||
return ra |
return ra |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,622: | Line 2,622: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">proc selectionSort[T](a: var openarray[T]) = |
||
let n = a.len |
let n = a.len |
||
for i in 0 ..< n: |
for i in 0 ..< n: |
||
Line 2,633: | Line 2,633: | ||
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] |
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] |
||
selectionSort a |
selectionSort a |
||
echo a</ |
echo a</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre> |
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let rec selection_sort = function |
||
[] -> [] |
[] -> [] |
||
| first::lst -> |
| first::lst -> |
||
Line 2,646: | Line 2,646: | ||
| x::xs -> select_r small (x::output) xs |
| x::xs -> select_r small (x::output) xs |
||
in |
in |
||
select_r first [] lst</ |
select_r first [] lst</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: selectSort(l) |
||
| b j i k s | |
| b j i k s | |
||
l size ->s |
l size ->s |
||
Line 2,660: | Line 2,660: | ||
k i b at b put i swap b put |
k i b at b put i swap b put |
||
] |
] |
||
b dup freeze ;</ |
b dup freeze ;</syntaxhighlight> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
< |
<syntaxhighlight lang="oorexx">/*REXX **************************************************************** |
||
* program sorts an array using the selection-sort method. |
* program sorts an array using the selection-sort method. |
||
* derived from REXX solution |
* derived from REXX solution |
||
Line 2,716: | Line 2,716: | ||
x.9='Aventine' |
x.9='Aventine' |
||
x.0=9 |
x.0=9 |
||
return</ |
return</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
Although lists are much more used in Oz than arrays, this algorithm seems more natural for arrays. |
Although lists are much more used in Oz than arrays, this algorithm seems more natural for arrays. |
||
< |
<syntaxhighlight lang="oz">declare |
||
proc {SelectionSort Arr} |
proc {SelectionSort Arr} |
||
proc {Swap K L} |
proc {Swap K L} |
||
Line 2,749: | Line 2,749: | ||
in |
in |
||
{SelectionSort A} |
{SelectionSort A} |
||
{Show {Array.toRecord unit A}}</ |
{Show {Array.toRecord unit A}}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">selectionSort(v)={ |
||
for(i=1,#v-1, |
for(i=1,#v-1, |
||
my(mn=i,t); |
my(mn=i,t); |
||
Line 2,763: | Line 2,763: | ||
); |
); |
||
v |
v |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 2,770: | Line 2,770: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
< |
<syntaxhighlight lang="perl">sub selection_sort |
||
{my @a = @_; |
{my @a = @_; |
||
foreach my $i (0 .. $#a - 1) |
foreach my $i (0 .. $#a - 1) |
||
Line 2,776: | Line 2,776: | ||
$a[$_] < $a[$min] and $min = $_ foreach $min .. $#a; |
$a[$_] < $a[$min] and $min = $_ foreach $min .. $#a; |
||
$a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];} |
$a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];} |
||
return @a;}</ |
return @a;}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
Line 2,802: | Line 2,802: | ||
<span style="color: #0000FF;">?</span><span style="color: #000000;">selection_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">selection_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,810: | Line 2,810: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Iterative: |
Iterative: |
||
< |
<syntaxhighlight lang="php">function selection_sort(&$arr) { |
||
$n = count($arr); |
$n = count($arr); |
||
for($i = 0; $i < count($arr); $i++) { |
for($i = 0; $i < count($arr); $i++) { |
||
Line 2,821: | Line 2,821: | ||
list($arr[$i],$arr[$min]) = array($arr[$min],$arr[$i]); |
list($arr[$i],$arr[$min]) = array($arr[$min],$arr[$i]); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Recursive: |
Recursive: |
||
< |
<syntaxhighlight lang="php">function selectionsort($arr,$result=array()){ |
||
if(count($arr) == 0){ |
if(count($arr) == 0){ |
||
return $result; |
return $result; |
||
Line 2,831: | Line 2,831: | ||
unset($arr[array_search(min($arr),$arr)]); |
unset($arr[array_search(min($arr),$arr)]); |
||
return selectionsort($arr,$nresult); |
return selectionsort($arr,$nresult); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de selectionSort (Lst) |
||
(map |
(map |
||
'((L) (and (cdr L) (xchg L (member (apply min @) L)))) |
'((L) (and (cdr L) (xchg L (member (apply min @) L)))) |
||
Lst ) |
Lst ) |
||
Lst )</ |
Lst )</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
Selection: procedure options (main); /* 2 November 2013 */ |
Selection: procedure options (main); /* 2 November 2013 */ |
||
Line 2,870: | Line 2,870: | ||
end Selection; |
end Selection; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results: |
Results: |
||
<pre> |
<pre> |
||
Line 2,878: | Line 2,878: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">Function SelectionSort( [Array] $data ) |
||
{ |
{ |
||
$datal=$data.length-1 |
$datal=$data.length-1 |
||
Line 2,897: | Line 2,897: | ||
} |
} |
||
$l = 100; SelectionSort( ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } ) )</ |
$l = 100; SelectionSort( ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } ) )</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Works with '''SWI-Prolog 6.3.11''' (needs nth0/4). |
Works with '''SWI-Prolog 6.3.11''' (needs nth0/4). |
||
<syntaxhighlight lang="prolog"> |
|||
<lang Prolog> |
|||
selection_sort([], []). |
selection_sort([], []). |
||
selection_sort([H | L], [H1 | L2]) :- |
selection_sort([H | L], [H1 | L2]) :- |
||
Line 2,919: | Line 2,919: | ||
nth0(Ind, L, H1, L2), |
nth0(Ind, L, H1, L2), |
||
nth0(Ind, L1, H, L2)). |
nth0(Ind, L1, H, L2)). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure selectionSort(Array a(1)) |
||
Protected i, j, lastIndex, minIndex |
Protected i, j, lastIndex, minIndex |
||
Line 2,935: | Line 2,935: | ||
Swap a(minIndex), a(i) |
Swap a(minIndex), a(i) |
||
Next |
Next |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">def selection_sort(lst): |
||
for i, e in enumerate(lst): |
for i, e in enumerate(lst): |
||
mn = min(range(i,len(lst)), key=lst.__getitem__) |
mn = min(range(i,len(lst)), key=lst.__getitem__) |
||
lst[i], lst[mn] = lst[mn], e |
lst[i], lst[mn] = lst[mn], e |
||
return lst</ |
return lst</syntaxhighlight> |
||
=={{header|Qi}}== |
=={{header|Qi}}== |
||
{{trans|sml}} |
{{trans|sml}} |
||
< |
<syntaxhighlight lang="qi">(define select-r |
||
Small [] Output -> [Small | (selection-sort Output)] |
Small [] Output -> [Small | (selection-sort Output)] |
||
Small [X|Xs] Output -> (select-r X Xs [Small|Output]) where (< X Small) |
Small [X|Xs] Output -> (select-r X Xs [Small|Output]) where (< X Small) |
||
Line 2,957: | Line 2,957: | ||
(selection-sort [8 7 4 3 2 0 9 1 5 6]) |
(selection-sort [8 7 4 3 2 0 9 1 5 6]) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ 0 swap |
||
behead swap |
behead swap |
||
witheach |
witheach |
||
Line 2,978: | Line 2,978: | ||
[] 20 times [ 10 random join ] |
[] 20 times [ 10 random join ] |
||
dup echo cr |
dup echo cr |
||
ssort echo cr</ |
ssort echo cr</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,988: | Line 2,988: | ||
=={{header|R}}== |
=={{header|R}}== |
||
For loop: |
For loop: |
||
< |
<syntaxhighlight lang="r">selectionsort.loop <- function(x) |
||
{ |
{ |
||
lenx <- length(x) |
lenx <- length(x) |
||
Line 2,998: | Line 2,998: | ||
} |
} |
||
x |
x |
||
}</ |
}</syntaxhighlight> |
||
Recursive: |
Recursive: |
||
(A prettier solution, but, you may need to increase the value of options("expressions") to test it. Also, you may get a stack overflow if the length of the input vector is more than a few thousand.) |
(A prettier solution, but, you may need to increase the value of options("expressions") to test it. Also, you may get a stack overflow if the length of the input vector is more than a few thousand.) |
||
< |
<syntaxhighlight lang="r">selectionsort.rec <- function(x) |
||
{ |
{ |
||
if(length(x) > 1) |
if(length(x) > 1) |
||
Line 3,009: | Line 3,009: | ||
c(x[mini], selectionsort(x[-mini])) |
c(x[mini], selectionsort(x[-mini])) |
||
} else x |
} else x |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ra}}== |
=={{header|Ra}}== |
||
<syntaxhighlight lang="ra"> |
|||
<lang Ra> |
|||
class SelectionSort |
class SelectionSort |
||
**Sort a list with the Selection Sort algorithm** |
**Sort a list with the Selection Sort algorithm** |
||
Line 3,046: | Line 3,046: | ||
list[i] := list[minCandidate] |
list[i] := list[minCandidate] |
||
list[minCandidate] := temp |
list[minCandidate] := temp |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(define (selection-sort xs) |
(define (selection-sort xs) |
||
Line 3,055: | Line 3,055: | ||
[else (define x0 (apply min xs)) |
[else (define x0 (apply min xs)) |
||
(cons x0 (selection-sort (remove x0 xs)))])) |
(cons x0 (selection-sort (remove x0 xs)))])) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
Solution 1: |
Solution 1: |
||
<lang |
<syntaxhighlight lang="raku" line>sub selection_sort ( @a is copy ) { |
||
for 0 ..^ @a.end -> $i { |
for 0 ..^ @a.end -> $i { |
||
my $min = [ $i+1 .. @a.end ].min: { @a[$_] }; |
my $min = [ $i+1 .. @a.end ].min: { @a[$_] }; |
||
Line 3,071: | Line 3,071: | ||
say 'input = ' ~ @data; |
say 'input = ' ~ @data; |
||
say 'output = ' ~ @data.&selection_sort; |
say 'output = ' ~ @data.&selection_sort; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,079: | Line 3,079: | ||
Solution 2: |
Solution 2: |
||
<lang |
<syntaxhighlight lang="raku" line>sub selectionSort(@tmp) { |
||
for ^@tmp -> $i { |
for ^@tmp -> $i { |
||
my $min = $i; @tmp[$i, $_] = @tmp[$_, $i] if @tmp[$min] > @tmp[$_] for $i^..^@tmp; |
my $min = $i; @tmp[$i, $_] = @tmp[$_, $i] if @tmp[$min] > @tmp[$_] for $i^..^@tmp; |
||
Line 3,085: | Line 3,085: | ||
return @tmp; |
return @tmp; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,093: | Line 3,093: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program sorts a stemmed array using the selection─sort algorithm. */ |
||
call init /*assign some values to an array: @. */ |
call init /*assign some values to an array: @. */ |
||
call show 'before sort' /*show the before array elements. */ |
call show 'before sort' /*show the before array elements. */ |
||
Line 3,119: | Line 3,119: | ||
return |
return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: do i=1 for #; say ' element' right(i,length(#)) arg(1)":" @.i; end; return</ |
show: do i=1 for #; say ' element' right(i,length(#)) arg(1)":" @.i; end; return</syntaxhighlight> |
||
{{out|output|:}} |
{{out|output|:}} |
||
<pre> |
<pre> |
||
Line 3,144: | Line 3,144: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
aList = [7,6,5,9,8,4,3,1,2,0] |
aList = [7,6,5,9,8,4,3,1,2,0] |
||
see sortList(aList) |
see sortList(aList) |
||
Line 3,164: | Line 3,164: | ||
next |
next |
||
return list |
return list |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby"> |
||
# a relatively readable version - creates a distinct array |
# a relatively readable version - creates a distinct array |
||
Line 3,223: | Line 3,223: | ||
puts "sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): #{sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0])}" |
puts "sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): #{sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0])}" |
||
# prints: sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
# prints: sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">siz = 10 |
||
dim srdData(siz) |
dim srdData(siz) |
||
for i = 1 to siz |
for i = 1 to siz |
||
Line 3,246: | Line 3,246: | ||
for i = 1 to siz |
for i = 1 to siz |
||
print i;chr$(9);srtData(i) |
print i;chr$(9);srtData(i) |
||
next i</ |
next i</syntaxhighlight> |
||
<pre>1 20.5576419 |
<pre>1 20.5576419 |
||
2 32.4299311 |
2 32.4299311 |
||
Line 3,259: | Line 3,259: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
fn selection_sort(array: &mut [i32]) { |
fn selection_sort(array: &mut [i32]) { |
||
Line 3,289: | Line 3,289: | ||
println!(" The sorted array is {:?}", array); |
println!(" The sorted array is {:?}", array); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">def swap(a: Array[Int], i1: Int, i2: Int) = { val tmp = a(i1); a(i1) = a(i2); a(i2) = tmp } |
||
def selectionSort(a: Array[Int]) = |
def selectionSort(a: Array[Int]) = |
||
for (i <- 0 until a.size - 1) |
for (i <- 0 until a.size - 1) |
||
swap(a, i, (i + 1 until a.size).foldLeft(i)((currMin, index) => |
swap(a, i, (i + 1 until a.size).foldLeft(i)((currMin, index) => |
||
if (a(index) < a(currMin)) index else currMin))</ |
if (a(index) < a(currMin)) index else currMin))</syntaxhighlight> |
||
This version avoids the extra definition by using a function literal: |
This version avoids the extra definition by using a function literal: |
||
< |
<syntaxhighlight lang="scala">def selectionSort(a: Array[Int]) = for (i <- 0 until a.size - 1) ( |
||
{ (i1: Int, i2: Int) => val tmp = a(i1); a(i1) = a(i2); a(i2) = tmp } |
{ (i1: Int, i2: Int) => val tmp = a(i1); a(i1) = a(i2); a(i2) = tmp } |
||
) (i, (i + 1 until a.size).foldLeft(i)((currMin, index) => if (a(index) < a(currMin)) index else currMin) )</ |
) (i, (i + 1 until a.size).foldLeft(i)((currMin, index) => if (a(index) < a(currMin)) index else currMin) )</syntaxhighlight> |
||
Functional way: |
Functional way: |
||
< |
<syntaxhighlight lang="scala">def selectionSort[T <% Ordered[T]](list: List[T]): List[T] = { |
||
def remove(e: T, list: List[T]): List[T] = |
def remove(e: T, list: List[T]): List[T] = |
||
list match { |
list match { |
||
Line 3,321: | Line 3,321: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">const proc: selectionSort (inout array elemType: arr) is func |
||
local |
local |
||
var integer: i is 0; |
var integer: i is 0; |
||
Line 3,342: | Line 3,342: | ||
arr[i] := help; |
arr[i] := help; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#selectionSort] |
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#selectionSort] |
||
Line 3,348: | Line 3,348: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">class Array { |
||
method selectionsort { |
method selectionsort { |
||
for i in ^(self.end) { |
for i in ^(self.end) { |
||
Line 3,367: | Line 3,367: | ||
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]; |
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]; |
||
say strs.selectionsort;</ |
say strs.selectionsort;</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">fun selection_sort [] = [] |
||
| selection_sort (first::lst) = |
| selection_sort (first::lst) = |
||
let |
let |
||
Line 3,382: | Line 3,382: | ||
in |
in |
||
small :: selection_sort output |
small :: selection_sort output |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">mata |
||
function selection_sort(real vector a) { |
function selection_sort(real vector a) { |
||
real scalar i, j, k, n |
real scalar i, j, k, n |
||
Line 3,397: | Line 3,397: | ||
} |
} |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func selectionSort(inout arr:[Int]) { |
||
var min:Int |
var min:Int |
||
Line 3,418: | Line 3,418: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{tcllib|struct::list}} |
{{tcllib|struct::list}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
package require struct::list |
package require struct::list |
||
Line 3,441: | Line 3,441: | ||
} |
} |
||
puts [selectionsort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</ |
puts [selectionsort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Line 3,472: | Line 3,472: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
<lang>PRINT "Selection sort:" |
<syntaxhighlight lang="text">PRINT "Selection sort:" |
||
n = FUNC (_InitArray) |
n = FUNC (_InitArray) |
||
PROC _ShowArray (n) |
PROC _ShowArray (n) |
||
Line 3,520: | Line 3,520: | ||
PRINT |
PRINT |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 3,527: | Line 3,527: | ||
is deleted from the list and inserted into another on each iteration |
is deleted from the list and inserted into another on each iteration |
||
rather than swapped with a preceding item of the same list. |
rather than swapped with a preceding item of the same list. |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
selection_sort "p" = @iNX ~&l->rx ^(gldif ==,~&r)^/~&l ^|C/"p"$- ~&</ |
selection_sort "p" = @iNX ~&l->rx ^(gldif ==,~&r)^/~&l ^|C/"p"$- ~&</syntaxhighlight> |
||
This is already a bad way to code a sorting algorithm in this |
This is already a bad way to code a sorting algorithm in this |
||
language, but with only a bit more work, we can get a bigger and |
language, but with only a bit more work, we can get a bigger and |
||
slower version that more closely simulates the operations of |
slower version that more closely simulates the operations of |
||
repeatedly reordering an array. |
repeatedly reordering an array. |
||
< |
<syntaxhighlight lang="ursala">selection_sort "p" = ~&itB^?a\~&a ^|JahPfatPRC/~& ~=-~BrhPltPClhPrtPCTlrTQrS^D/"p"$- ~&</syntaxhighlight> |
||
Here is a test program sorting by the partial order relation on natural |
Here is a test program sorting by the partial order relation on natural |
||
numbers. |
numbers. |
||
< |
<syntaxhighlight lang="ursala">#import nat |
||
#cast %nL |
#cast %nL |
||
example = selection_sort(nleq) <294,263,240,473,596,392,621,348,220,815></ |
example = selection_sort(nleq) <294,263,240,473,596,392,621,348,220,815></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre><220,240,263,294,348,392,473,596,621,815></pre> |
<pre><220,240,263,294,348,392,473,596,621,815></pre> |
||
Line 3,547: | Line 3,547: | ||
I shameless stole the swap function from the bubblesort VBscript implementation. |
I shameless stole the swap function from the bubblesort VBscript implementation. |
||
<syntaxhighlight lang="vba"> |
|||
<lang VBA> |
|||
sub swap( byref a, byref b) |
sub swap( byref a, byref b) |
||
dim tmp |
dim tmp |
||
Line 3,566: | Line 3,566: | ||
selectionSort = a |
selectionSort = a |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">Function Selection_Sort(s) |
||
arr = Split(s,",") |
arr = Split(s,",") |
||
For i = 0 To UBound(arr) |
For i = 0 To UBound(arr) |
||
Line 3,587: | Line 3,587: | ||
WScript.StdOut.Write "3,2,5,4,1" & vbTab & Selection_Sort("3,2,5,4,1") |
WScript.StdOut.Write "3,2,5,4,1" & vbTab & Selection_Sort("3,2,5,4,1") |
||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
WScript.StdOut.Write "c,e,b,a,d" & vbTab & Selection_Sort("c,e,b,a,d")</ |
WScript.StdOut.Write "c,e,b,a,d" & vbTab & Selection_Sort("c,e,b,a,d")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,597: | Line 3,597: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ecmascript">var selectionSort = Fn.new { |a| |
||
var last = a.count - 1 |
var last = a.count - 1 |
||
for (i in 0...last) { |
for (i in 0...last) { |
||
Line 3,620: | Line 3,620: | ||
System.print("After : %(a)") |
System.print("After : %(a)") |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,633: | Line 3,633: | ||
Alternatively we can just call a library method. |
Alternatively we can just call a library method. |
||
{{libheader|Wren-sort}} |
{{libheader|Wren-sort}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/sort" for Sort |
||
var as = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ] |
var as = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ] |
||
Line 3,641: | Line 3,641: | ||
System.print("After : %(a)") |
System.print("After : %(a)") |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,649: | Line 3,649: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
string 0; \use zero-terminated strings |
string 0; \use zero-terminated strings |
||
Line 3,674: | Line 3,674: | ||
SelSort(Str, StrLen(Str)); |
SelSort(Str, StrLen(Str)); |
||
Text(0, Str); CrLf(0); |
Text(0, Str); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,682: | Line 3,682: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn selectionSort(list){ // sort a list of ints |
||
copy,r:=list.copy(),List(); |
copy,r:=list.copy(),List(); |
||
while(copy){ |
while(copy){ |
||
Line 3,690: | Line 3,690: | ||
} |
} |
||
r |
r |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">selectionSort(List(28, 44, 46, 24, 19, -5, 2, 17, 11, 25, 4)).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |