Sorting algorithms/Selection sort: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 184: Line 184:
big fjords vex quick waltz nymph
big fjords vex quick waltz nymph
</pre>
</pre>

=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
Line 439: Line 440:
sorted .= "," . a%A_Index%
sorted .= "," . a%A_Index%
Return SubStr(sorted,2) ; drop leading comma
Return SubStr(sorted,2) ; drop leading comma
}</lang>
}</lang>


=={{header|AWK}}==
=={{header|AWK}}==
Line 516: Line 517:
4 65 2 -31 0 99 2 83 782 1
4 65 2 -31 0 99 2 83 782 1
-31 0 1 2 2 4 65 83 99 782
-31 0 1 2 2 4 65 83 99 782
</pre>

=={{header|C++}}==
Uses C++11. Compile with
g++ -std=c++11 selection.cpp
<lang cpp>#include <algorithm>
#include <iterator>
#include <iostream>

template<typename ForwardIterator> void selection_sort(ForwardIterator begin,
ForwardIterator end) {
for(auto i = begin; i != end; ++i) {
std::iter_swap(i, std::min_element(i, end));
}
}

int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
selection_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}</lang>
{{out}}
<pre>
-199 -52 2 3 33 56 99 100 177 200
</pre>
</pre>


Line 587: Line 563:
test
test
this</pre>
this</pre>

=={{header|C++}}==
Uses C++11. Compile with
g++ -std=c++11 selection.cpp
<lang cpp>#include <algorithm>
#include <iterator>
#include <iostream>

template<typename ForwardIterator> void selection_sort(ForwardIterator begin,
ForwardIterator end) {
for(auto i = begin; i != end; ++i) {
std::iter_swap(i, std::min_element(i, end));
}
}

int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
selection_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}</lang>
{{out}}
<pre>
-199 -52 2 3 33 56 99 100 177 200
</pre>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 691: Line 692:
> (selection-sort (vector 8 7 4 3 2 0 9 1 5 6) '>)
> (selection-sort (vector 8 7 4 3 2 0 9 1 5 6) '>)
#(9 8 7 6 5 4 3 2 1 0)</pre>
#(9 8 7 6 5 4 3 2 1 0)</pre>



=={{header|Crystal}}==
=={{header|Crystal}}==
Line 889: Line 889:
}
}
}</lang>
}</lang>

=={{header|EasyLang}}==
=={{header|EasyLang}}==


Line 945: Line 946:
→ #( 2 3 4 5 6 8 9)
→ #( 2 3 4 5 6 8 9)
</lang>
</lang>



=={{header|Eiffel}}==
=={{header|Eiffel}}==
Line 1,074: Line 1,074:
Sorted: -7 1 1 3 5 7 27 32 99
Sorted: -7 1 1 3 5 7 27 32 99
</pre>
</pre>

=={{header|Elena}}==
=={{header|Elena}}==
ELENA 5.0 :
ELENA 5.0 :
Line 1,131: Line 1,132:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
</pre>

=={{header|Erlang}}==
=={{header|Erlang}}==
<lang Erlang>
<lang Erlang>
Line 1,150: Line 1,152:
print_array(Ans).
print_array(Ans).
</lang>
</lang>



=={{header|Euphoria}}==
=={{header|Euphoria}}==
Line 1,293: Line 1,294:
Sorted array : -5 -2 0 1 3 4 6 7 8 9
Sorted array : -5 -2 0 1 3 4 6 7 8 9
</pre>
</pre>

=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 03-12-2016
<lang freebasic>' version 03-12-2016
Line 1,402: Line 1,404:
Sorted: -97, -84, -82, -82, -75, -64, -60, -31, -30, -26, -20, -15, -11, 8, 19, 36, 37, 55, 73, 81, 94
Sorted: -97, -84, -82, -82, -75, -64, -60, -31, -30, -26, -20, -15, -11, 8, 19, 36, 37, 55, 73, 81, 94
</pre>
</pre>

=={{header|GAP}}==
=={{header|GAP}}==
<lang gap>SelectionSort := function(v)
<lang gap>SelectionSort := function(v)
Line 1,538: Line 1,541:
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths]
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths]
</pre>
</pre>

=={{header|Io}}==
<lang io>List do (
selectionSortInPlace := method(
size repeat(idx,
swapIndices(idx, indexOf(slice(idx, size) min))
)
)
)

l := list(-1, 4, 2, -9)
l selectionSortInPlace println # ==> list(-9, -1, 2, 4)</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 1,579: Line 1,570:
on string : "qwerty"
on string : "qwerty"
with op = &null: "eqrtwy" (0 ms)</pre>
with op = &null: "eqrtwy" (0 ms)</pre>

=={{header|Io}}==
<lang io>List do (
selectionSortInPlace := method(
size repeat(idx,
swapIndices(idx, indexOf(slice(idx, size) min))
)
)
)

l := list(-1, 4, 2, -9)
l selectionSortInPlace println # ==> list(-9, -1, 2, 4)</lang>


=={{header|IS-BASIC}}==
=={{header|IS-BASIC}}==
Line 1,711: Line 1,714:
]
]
</pre>
</pre>

=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}
Line 1,950: Line 1,954:
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</lang>
print data</lang>

=={{header|Nanoquery}}==
{{trans|Java}}
<lang Nanoquery>import math

def sort(nums)
global math
for currentPlace in range(0, len(nums) - 2)
smallest = math.maxint
smallestAt = currentPlace + 1
for check in range(currentPlace, len(nums) - 1)
if nums[check] < smallest
smallestAt = check
smallest = nums[check]
end
end
temp = nums[currentPlace]
nums[currentPlace] = nums[smallestAt]
nums[smallestAt] = temp
end
return nums
end</lang>


=={{header|N/t/roff}}==
=={{header|N/t/roff}}==
Line 2,038: Line 2,020:
483
483
589</lang>
589</lang>

=={{header|Nanoquery}}==
{{trans|Java}}
<lang Nanoquery>import math

def sort(nums)
global math
for currentPlace in range(0, len(nums) - 2)
smallest = math.maxint
smallestAt = currentPlace + 1
for check in range(currentPlace, len(nums) - 1)
if nums[check] < smallest
smallestAt = check
smallest = nums[check]
end
end
temp = nums[currentPlace]
nums[currentPlace] = nums[smallestAt]
nums[smallestAt] = temp
end
return nums
end</lang>


=={{header|Nemerle}}==
=={{header|Nemerle}}==
Line 2,304: Line 2,308:
$a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];}
$a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];}
return @a;}</lang>
return @a;}</lang>

=={{header|Perl 6}}==
Solution 1:
<lang perl6>sub selection_sort ( @a is copy ) {
for 0 ..^ @a.end -> $i {
my $min = [ $i+1 .. @a.end ].min: { @a[$_] };
@a[$i, $min] = @a[$min, $i] if @a[$i] > @a[$min];
}
return @a;
}

my @data = 22, 7, 2, -5, 8, 4;
say 'input = ' ~ @data;
say 'output = ' ~ @data.&selection_sort;
</lang>

{{out}}
<pre>input = 22 7 2 -5 8 4
output = -5 2 4 7 8 22
</pre>

Solution 2:
<lang perl6>sub selectionSort(@tmp) {
for ^@tmp -> $i {
my $min = $i; @tmp[$i, $_] = @tmp[$_, $i] if @tmp[$min] > @tmp[$_] for $i^..^@tmp;
}
return @tmp;
}
</lang>

{{out}}
<pre>input = 22 7 2 -5 8 4
output = -5 2 4 7 8 22
</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,467: Line 2,437:
nth0(Ind, L1, H, L2)).
nth0(Ind, L1, H, L2)).
</lang>
</lang>

=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure selectionSort(Array a(1))
<lang PureBasic>Procedure selectionSort(Array a(1))
Line 2,490: Line 2,461:
lst[i], lst[mn] = lst[mn], e
lst[i], lst[mn] = lst[mn], e
return lst</lang>
return lst</lang>

=={{header|Qi}}==
{{trans|sml}}
<lang qi>(define select-r
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 Small Xs [X|Output]))

(define selection-sort
[] -> []
[First|Lst] -> (select-r First Lst []))

(selection-sort [8 7 4 3 2 0 9 1 5 6])
</lang>


=={{header|R}}==
=={{header|R}}==
Line 2,515: Line 2,500:
} else x
} else x
}</lang>
}</lang>

=={{header|Qi}}==
{{trans|sml}}
<lang qi>(define select-r
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 Small Xs [X|Output]))

(define selection-sort
[] -> []
[First|Lst] -> (select-r First Lst []))

(selection-sort [8 7 4 3 2 0 9 1 5 6])
</lang>

=={{header|Racket}}==
<lang racket>
#lang racket
(define (selection-sort xs)
(cond [(empty? xs) '()]
[else (define x0 (apply min xs))
(cons x0 (selection-sort (remove x0 xs)))]))
</lang>


=={{header|Ra}}==
=={{header|Ra}}==
Line 2,575: Line 2,537:
list[minCandidate] := temp
list[minCandidate] := temp
</lang>
</lang>

=={{header|Racket}}==
<lang racket>
#lang racket
(define (selection-sort xs)
(cond [(empty? xs) '()]
[else (define x0 (apply min xs))
(cons x0 (selection-sort (remove x0 xs)))]))
</lang>

=={{header|Raku}}==
(formerly Perl 6)
Solution 1:
<lang perl6>sub selection_sort ( @a is copy ) {
for 0 ..^ @a.end -> $i {
my $min = [ $i+1 .. @a.end ].min: { @a[$_] };
@a[$i, $min] = @a[$min, $i] if @a[$i] > @a[$min];
}
return @a;
}

my @data = 22, 7, 2, -5, 8, 4;
say 'input = ' ~ @data;
say 'output = ' ~ @data.&selection_sort;
</lang>

{{out}}
<pre>input = 22 7 2 -5 8 4
output = -5 2 4 7 8 22
</pre>

Solution 2:
<lang perl6>sub selectionSort(@tmp) {
for ^@tmp -> $i {
my $min = $i; @tmp[$i, $_] = @tmp[$_, $i] if @tmp[$min] > @tmp[$_] for $i^..^@tmp;
}
return @tmp;
}
</lang>

{{out}}
<pre>input = 22 7 2 -5 8 4
output = -5 2 4 7 8 22
</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 2,824: Line 2,830:
small :: selection_sort output
small :: selection_sort output
end</lang>
end</lang>

=={{header|Stata}}==
=={{header|Stata}}==
<lang stata>mata
<lang stata>mata
Line 2,838: Line 2,845:
}
}
end</lang>
end</lang>

=={{header|Swift}}==
=={{header|Swift}}==
<lang Swift>func selectionSort(inout arr:[Int]) {
<lang Swift>func selectionSort(inout arr:[Int]) {
Line 2,960: Line 2,968:
PRINT
PRINT
RETURN</lang>
RETURN</lang>

=={{header|Ursala}}==
=={{header|Ursala}}==
The selection_sort function is parameterized by a relational predicate p.
The selection_sort function is parameterized by a relational predicate p.
Line 3,005: Line 3,014:
end function
end function
</lang>
</lang>

=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vb>Function Selection_Sort(s)
<lang vb>Function Selection_Sort(s)