Jump to content

Sorting algorithms/Selection sort: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 184:
big fjords vex quick waltz nymph
</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
Line 439 ⟶ 440:
sorted .= "," . a%A_Index%
Return SubStr(sorted,2) ; drop leading comma
}</lang>
 
=={{header|AWK}}==
Line 516 ⟶ 517:
4 65 2 -31 0 99 2 83 782 1
-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>
 
Line 587 ⟶ 563:
test
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}}==
Line 691 ⟶ 692:
> (selection-sort (vector 8 7 4 3 2 0 9 1 5 6) '>)
#(9 8 7 6 5 4 3 2 1 0)</pre>
 
 
=={{header|Crystal}}==
Line 889:
}
}</lang>
 
=={{header|EasyLang}}==
 
Line 945 ⟶ 946:
→ #( 2 3 4 5 6 8 9)
</lang>
 
 
=={{header|Eiffel}}==
Line 1,074:
Sorted: -7 1 1 3 5 7 27 32 99
</pre>
 
=={{header|Elena}}==
ELENA 5.0 :
Line 1,131 ⟶ 1,132:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
 
=={{header|Erlang}}==
<lang Erlang>
Line 1,150 ⟶ 1,152:
print_array(Ans).
</lang>
 
 
=={{header|Euphoria}}==
Line 1,293 ⟶ 1,294:
Sorted array : -5 -2 0 1 3 4 6 7 8 9
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 03-12-2016
Line 1,402 ⟶ 1,404:
Sorted: -97, -84, -82, -82, -75, -64, -60, -31, -30, -26, -20, -15, -11, 8, 19, 36, 37, 55, 73, 81, 94
</pre>
 
=={{header|GAP}}==
<lang gap>SelectionSort := function(v)
Line 1,538 ⟶ 1,541:
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths]
</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}}==
Line 1,579 ⟶ 1,570:
on string : "qwerty"
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}}==
Line 1,711 ⟶ 1,714:
]
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 1,950 ⟶ 1,954:
data = selectionSort #(4, 9, 3, -2, 0, 7, -5, 1, 6, 8)
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}}==
Line 2,038 ⟶ 2,020:
483
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}}==
Line 2,304 ⟶ 2,308:
$a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];}
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}}==
Line 2,467 ⟶ 2,437:
nth0(Ind, L1, H, L2)).
</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>Procedure selectionSort(Array a(1))
Line 2,490 ⟶ 2,461:
lst[i], lst[mn] = lst[mn], e
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}}==
Line 2,515 ⟶ 2,500:
} else x
}</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}}==
Line 2,575 ⟶ 2,537:
list[minCandidate] := temp
</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}}==
Line 2,824 ⟶ 2,830:
small :: selection_sort output
end</lang>
 
=={{header|Stata}}==
<lang stata>mata
Line 2,838 ⟶ 2,845:
}
end</lang>
 
=={{header|Swift}}==
<lang Swift>func selectionSort(inout arr:[Int]) {
Line 2,960 ⟶ 2,968:
PRINT
RETURN</lang>
 
=={{header|Ursala}}==
The selection_sort function is parameterized by a relational predicate p.
Line 3,005 ⟶ 3,014:
end function
</lang>
 
=={{header|VBScript}}==
<lang vb>Function Selection_Sort(s)
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.