Jump to content

Gnome sort: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m fixed redirect
No edit summary
Line 20: Line 20:


==Implementations==
==Implementations==

===[[C Sharp|C#]]===
===[[C]]===
void GnomeSort( int[] array )
void gnome_sort(int *array, int size) {
{
for ( int i = 1, temp_value; i < array.Length; )
int i;
int tmp;
{
if ( array[i-1] <= array[i] )
for (i = 1; i < size; ) {
i += 1;
if (array[i - 1] <= array[i] ) {
else
++i;
{
} else {
temp_value = array[i-1];
tmp = array[i];
array[i-1] = array[i];
array[i] = array[i - 1];
array[i] = temp_value;
array[i - 1] = tmp;
i -= 1;
--i;
if ( i == 0 )
if ( i == 0 ) {
i = 1;
i = 1;
}
}
}
}
}
}
}


===[[C++]]===
===[[C++]]===
Line 57: Line 58:
}
}


===[[Perl programming language|Perl]]===
===[[C Sharp|C#]]===
void GnomeSort( int[] array )
sub gnome_sort(@)
{
{
for ( int i = 1, temp_value; i < array.Length; )
my @a = @_;
my $i = 1;
{
while ($i < @a) {
if ( array[i-1] <= array[i] )
if ($a[$i-1] <= $a[$i]) {
i += 1;
$i++;
else
}
{
temp_value = array[i-1];
else {
@a[$i-1, $i] = @a[$i, $i-1];
array[i-1] = array[i];
$i--;
array[i] = temp_value;
$i = 1 if $i == 0;
i -= 1;
if ( i == 0 )
i = 1;
}
}
}
}
return @a;
}
}

===[[Python programming language|Python]]===
def gnome_sort(L):
L = L[:]
i = 1
while i < len(L):
if L[i-1] <= L[i]:
i += 1
else:
L[i], L[i-1] = L[i-1], L[i]
i -= 1
if i == 0:
i = 1
return L


===[[FORTRAN 77]]===
===[[FORTRAN 77]]===
Line 109: Line 97:
END
END


===[[java programming language|Java]]===
===[[Java programming language|Java]]===
public class GnomeSort {
public class GnomeSort {
static void gnomeSort( int[] theArray ) {
static void gnomeSort( int[] theArray ) {
Line 128: Line 116:
}
}



===[[C]]===

void gnome_sort(int *array, int size) {
===[[Perl programming language|Perl]]===
int i;
sub gnome_sort(@)
int tmp;
{
for (i = 1; i < size; ) {
my @a = @_;
if (array[i - 1] <= array[i] ) {
++i;
my $i = 1;
} else {
while ($i < @a) {
tmp = array[i];
if ($a[$i-1] <= $a[$i]) {
array[i] = array[i - 1];
$i++;
array[i - 1] = tmp;
--i;
if ( i == 0 ) {
i = 1;
}
}
}
}
}
else {
@a[$i-1, $i] = @a[$i, $i-1];
$i--;
$i = 1 if $i == 0;
}
}
return @a;
}

===[[PHP programming language|PHP]]===
<?php
function gnome_sort($list)
{
for($i = 1; $i < count($list);)
{
if ($list["$i"-1] <= $list["$i"]) {$i++;}
else
{
/* Use bitwise XORing to swap the two elements */
$list["$i"] ^= $list["$i"-1];
$list["$i"-1] ^= $list["$i"];
$list["$i"] ^= $list["$i"-1];
$i--;
if ($i == 0) {$i = 1;}
}
}
return $list;
}
?>

===[[Python programming language|Python]]===
def gnome_sort(L):
i = 1
while i < len(L):
if L[i-1] <= L[i]:
i += 1
else:
L[i], L[i-1] = L[i-1], L[i]
i -= 1
if i == 0:
i = 1
return L

===[[Ruby programming language|Ruby]] ===
def gnome_sort(list)
i = 1
while i < list.length()
if list[i-1] <= list[i]
i += 1
else
list[i], list[i-1] = list[i-1], list[i]
i -= 1
if i == 0
i = 1
end
end
end
return list
end


==External link==
==External link==

Revision as of 17:27, 26 May 2006

Gnome sort is a sorting algorithm which is similar to insertion sort except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. The name comes from the supposed behavior of the Dutch garden gnome in sorting a line of flowerpots. It is conceptually simple, requiring no nested loops. The running time is O(n2), and in practice the algorithm has been reported to run slightly slower than bubble sort and sometimes faster, although this depends on the details of the architecture and the implementation.

Description

Here is pseudocode for the sort:

 function gnomeSort(a[1..size]) {
  i := 2
  while i ≤ size
    if a[i-1] ≤ a[i]
        i := i + 1
    else
        swap a[i-1] and a[i]
        i := i - 1
        if i = 1
          i := 2
 }

Effectively, the algorithm always finds the first place where two adjacent elements are in the wrong order, and swaps them. If this place were searched for naively, the result would be O(n3). Instead, it takes advantage of the fact that performing a swap can only introduce a new out-of-order adjacent pair right before the two swapped elements, and so checks this position immediately after performing such a swap.

Implementations

    void gnome_sort(int *array, int size) {
       int i;
       int tmp;
       for (i = 1; i < size; ) {
          if (array[i - 1] <= array[i] ) {
             ++i;
          } else {
             tmp = array[i];
             array[i] = array[i - 1];
             array[i - 1] = tmp;
             --i;
             if ( i == 0 ) {
                i = 1;
             }		
          }
       }
    }
 #include <algorithm> // for std::swap
 void gnomeSort( int* array, int size )
 {
   for ( int i = 1; i < size; ) {
     if ( array[i-1] <= array[i] ) {
       ++i;
     }
     else {
       std::swap( array[i-1], array[i] );
       --i;
       if ( i == 0 ) {
         i = 1;
       }
     }
   }
 }
void GnomeSort( int[] array )
{
  for ( int i = 1, temp_value; i < array.Length; )
  {
    if ( array[i-1] <= array[i] )
      i += 1;
    else
    {
      temp_value = array[i-1];
      array[i-1] = array[i];
      array[i] = temp_value;
      i -= 1;
      if ( i == 0 )
        i = 1;
    }
  }
}
 SUBROUTINE gnome_sort(A, LEN)
 INTEGER A, LEN, I, TEMP
 DIMENSION A(LEN)
 I = 2
 WHILE (I .LE. LEN) DO
   IF ((I .EQ. 1) .OR. (A(I-1) .LE. A(I))) THEN
     I = I + 1
   ELSE
     TEMP = A(I)
     A(I) = A(I-1)
     A(I-1) = TEMP
     I = I - 1
     IF (I .EQ. 1) THEN
       I = 2
     END IF
   END IF
 END WHILE
 END
 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;
             }		
          }
       }
    }
 }


sub gnome_sort(@)
{
  my @a = @_;
  my $i = 1;
  while ($i < @a) {
    if ($a[$i-1] <= $a[$i]) {
      $i++;
    }
    else {
      @a[$i-1, $i] = @a[$i, $i-1];
      $i--;
      $i = 1 if $i == 0;
    }
  }
  return @a;
}
<?php
function gnome_sort($list)
{
    for($i = 1; $i < count($list);)
    {
        if ($list["$i"-1] <= $list["$i"]) {$i++;}
        else
        {
        /* Use bitwise XORing to swap the two elements */
            $list["$i"]   ^= $list["$i"-1];
            $list["$i"-1] ^= $list["$i"];
            $list["$i"]   ^= $list["$i"-1];
            $i--;
            if ($i == 0) {$i = 1;}
        }
    }
    return $list;
}
?>
def gnome_sort(L):
  i = 1
  while i < len(L):
    if L[i-1] <= L[i]:
      i += 1
    else:
      L[i], L[i-1] = L[i-1], L[i]
      i -= 1
      if i == 0:
        i = 1
  return L
def gnome_sort(list)
    i = 1
    while i < list.length()
        if list[i-1] <= list[i]
            i += 1
        else
            list[i], list[i-1] = list[i-1], list[i]
            i -= 1
            if i == 0
                i = 1
            end
        end
    end
    return list
end