Gnome sort
From Algorithm wiki
| |
Contents |
[edit] Pseudocode
function gnomeSort(a[0..size-1]) {
i := 1
j := 2
while i = size - 1
if a[i-1] <= a[i]
i := j
j := j + 1
else
swap a[i-1] and a[i]
i := i - 1
if i = 0
i := 1
}
[edit] C Code
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Argument are a integer array and its size
int[] gnomeSort(int a[], int size)
{
int i = 1;
while(i < size)
{
if( a[i-1] <=a[i])
{
i++;
{
else
{
swap(& a[i-1], & a[i]);
i--;
if(i == 0) i = 1;
}
}
}
[edit] C++ Code
template <typename Type>
void Gnome(Type *pF, Type *pL)
{
Type *pI = pF+1;
while (pI < pL) {
if (*(pI-1) <= *pI) {
pI++;
} else {
iter_swap(pI-1, pI); // <algorithm>
if (--pI == pF) { pI = pF+1; }
}
}
}
[edit] PHP Code
<?php
function gnomeSort($arr, $arrSize = null)
{
$i = 0;
if ($arrSize === null) $arrSize = count($arr);
while ($i < $arrSize)
{
if ($i = 0 || $arr[$i - 1] <= $arr[$i]) $i++;
else
{
$tmp = $arr[$i - 1];
$arr[$i - 1] = $arr[$i];
$arr[$i] = $tmp;
$i--;
}
}
}
?>
[edit] C# Code
This section needs your Code!
[edit] Java Code
This section needs your Code!
- public class GnomeSort{
static void gnomeSort(int[] theArray) { for (int index = 1; index < theArray.length;) { if (theArray[index - 1] <= theArray[index]) { ++index; }else{ int tempVal = theArray[index]; theArray[index] = theArray[index - 1]; theArray[index - 1] = tempVal; --index; if (index == 0) { index = 1; } } } } }
[edit] vb.net
Module Module1
Private Sub swap(ByRef a As Integer, ByRef b As Integer)
Dim temp As Integer
temp = a
a = b
b = temp
End Sub
Private Sub gnomeSort(ByVal a() As Integer)
Dim i As Integer
i = 2
While (i <= UBound(a))
If (a(i - 1) <= a(i)) Then
i = i + 1
Else
swap(a(i - 1), a(i))
i = i - 1
If i = 1 Then
i = 2
End If
End If
End While
End Sub
Sub Main()
Dim vect(50000) As Integer
Dim X As Integer
For x = 1 To UBound(vect)
vect(X) = CInt(Rnd() * 50)
Next
gnomeSort(vect)
For X = 1 To UBound(vect)
Console.WriteLine(vect(X))
Next
Console.ReadKey()
End Sub
End Module
[edit] Perl Code
This section needs your Code!
[edit] Prolog Code
This section needs your Code!
[edit] Python Code
def gnomeSort(array):
i = 1
j = 2
while i < len(array):
if array[i-1] <= array[i]:
i = j
j += 1
else:
array[i-1],array[i] = array[i],array[i-1]
i -= 1
if i==0:
i=1
[edit] Ruby Code
module GnomeSort
def self.sort(keys)
sort!(keys.clone)
end
def self.sort!(keys)
i, j = 1, 2
while i < keys.size
if keys[i-1] <= keys[i]
i, j = j, j+1
else
keys[i-1], keys[i] = keys[i], keys[i-1]
i -= 1 if i > 1
end
end
keys
end
end
[edit] Pascal/Delphi Code
procedure GnomeSort(var A:array of Integer);
var
i, j, z: Integer;
begin
i := Low(A) + 1;
j := i + 1;
while(i <= High(A))do
begin
if(A[i-1] <= A[i])then
begin
i := j;
j := j + 1;
end
else
begin
z := A[i-1];
A[i-1] := A[i];
A[i] := z;
i := i - 1;
if(i = 0)then i := 1;
end;
end;
end;
[edit] Basic Code
By Dave Stratford
DEF PROC_GnomeSort1(Size%)
I%=2
J%=2
REPEAT
IF data%(J%-1) <=data%(J%) THEN
I%+=1
J%=I%
ELSE
SWAP data%(J%-1),data%(J%)
J%-=1
IF J%=1 THEN
I%+=1
J%=I%
ENDIF
ENDIF
UNTIL I%>Size%
ENDPROC

