Selection sort
From Algorithm wiki
| |
Contents |
[edit] PHP Code
function selection_sort($arr){
$count = count($arr);
for ($i = 0; $i < $count; $i++) {
$k = $i;
for ($j = $i + 1; $j < $count; $j++){
if ($arr[$k] > $arr[$j]) {
$k = $j;
}
}
$tmp = $arr[$i];
$arr[$i] = $arr[$k];
$arr[$k] = $tmp;
}
return $arr;
}
[edit] C Code
void selectionSort(int numbers[], int array_size) {
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++) {
min = i;
for (j = i+1; j < array_size; j++)
if (numbers[j] < numbers[min])
min = j;
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
[edit] C++ Code
[edit] int
void SelectionSort(int a[], int n)
{
if(a==NULL)
return;
for(int i=0; i<n-1; ++i)
{
int minIndex = i;
for(int j=i+1; j<n; ++j)
if(a[j]<a[minIndex])
minIndex = j;
int t = a[minIndex];
a[minIndex] = a[i];
a[i] = t;
}
}
[edit] template
template <typename Type> inline
void Selection(Type *pF, Type *pL)
{
Type *pMin;
Type *pI, *pJ;
for (pI=pF-1; ++pI<pL-1;) {
pMin=pI;
for (pJ=pI; ++pJ!=pL;)
if (*pJ < *pMin)
pMin = pJ;
iter_swap(pMin, pI);
}
}
[edit] Java Code
[edit] Java Code1
public void Simple_Selection_Sort(int[] array) {
for (int i = 0, k = 0, temp = 0; i < array.length - 1; i++) {
k = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[k])
k = j;
}
if (k != i) {
temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
}
[edit] Java Code2
class SelectionSortAlgorithm extends SortAlgorithm {
void sort(int a[]) throws Exception {
for (int i = a.length-1; i > 0; i--) {
int T = 0;
for (int j = 1; j <= i; j++) {
if(stopRequested) {
return;
}
if(a[j] > a[T]) {
T = j;
}
compex(j, T);
pause(i);
}
int temp = a[i];
a[i] = a[T];
a[T] = temp;
}
}
}
[edit] Pseudocode
SelectionSort(A)
// GOAL: place the elements of A in ascending order
1 n := length[A]
2 for i := 1 to n
3 // GOAL: place the correct number in A[i]
4 j := FindIndexOfSmallest( A, i, n )
5 swap A[i] with A[j]
// L.I. A[1..i] the i smallest numbers sorted
6 end-for
7 end-procedure
FindIndexOfSmallest( A, i, n )
// GOAL: return j in the range [i,n] such
// that A[j]<=A[k] for all k in range [i,n]
1 smallestAt := i ;
2 for j := (i+1) to n
3 if ( A[j] < A[smallestAt] ) smallestAt := j
// L.I. A[smallestAt] smallest among A[i..j]
4 end-for
5 return smallestAt
6 end-procedure
[edit] C++ Code
const int fieldSize = 100;
fieldPointer = new int[fieldSize];
void selectsort()
{
int min;
for(int i = 0; i < fieldSize; i++){
min = i;
for(int j = i+1; j < fieldSize; j++)
{
if(fieldPointer[j] < fieldPointer[min])
min = j;
}
if(min != i)
{
swap(fieldPointer[min],fieldPointer[i]);
}
}
}
[edit] VB Code
This section needs your Code!
[edit] Pascal/Delphi Code
procedure SelectionSort(var A:array of Integer);
Var
i, j, Selected, temp: Integer;
begin
for i:= 0 to pred(high(A)) do
begin
Selected := i;
for j := i + 1 to high(A) do
if(A[Selected] > A[j])then
Selected := j; //remember best key
//Swap with the selected value
temp := A[i];
A[i] := A[Selected];
A[Selected] := temp;
end;
end;
for(my $i = 0; $i< $#array; $i++) { $min = $i; for(my $j = $i+1; $j <= $#array; $j++) { if($array[$j] < $array[$min]) { $min = $j; } }
if($min != $i) { my $temp1; $temp1 = $array[$i]; $array[$i] = $array[$min]; $array[$min] = $temp1; }
}
[edit] Prolog Code
findmin([X], X). findmin([H,X|T], N):- H < X, findmin([H|T], N). findmin([H,X|T], Z):- X < H, findmin([X|T], Z). remove(X, [X|T], T). remove(X, [H|T], [H|R]):- X \= H, remove(X, T, R). selectionsort([], []). selectionsort(R, [X|Z]):- findmin(R, X), remove(X, R, W), selectionsort(W, Z).
[edit] Python Code
def selectionSort(l): for i in xrange(len(l)): min=i for j in xrange(i+1,len(l)): if(l[j]<l[min]): min=j temp=l[i] l[i]=l[min] l[min]=temp return l
[edit] Ruby Code
def selection_sort!(ary)
for i in 0..(ary.size-2)
min = ary[i..-1].min
j = i + ary[i..-1].index(min)
ary[i], ary[j] = ary[j], ary[i]
end
end
[edit] Basic Code
By Dave Stratford
DEF PROC_SelectionSort(Size%)
FOR I% = 1 TO Size%-1
lowest% = I%
FOR J% = (I% + 1) TO Size%
IF data%(J%) < data%(lowest%) lowest% = J%
NEXT J%
IF I%<>lowest% SWAP data%(I%),data%(lowest%)
NEXT I%
ENDPROC

