WeixiangYan/CodeTransOcean huggingface dataset:
Task: Sorting algorithms_Gnome sort



C#:         public static void gnomeSort(int[] anArray)
        {
            int first = 1;
            int second = 2;

            while (first < anArray.Length)
            {
                if (anArray[first - 1] <= anArray[first])
                {
                    first = second;
                    second++;
                }
                else
                {
                    int tmp = anArray[first - 1];
                    anArray[first - 1] = anArray[first];
                    anArray[first] = tmp;
                    first -= 1;
                    if (first == 0)
                    {
                        first = 1;
                        second = 2;
                    }
                }
                
            }
        }
Task: Sorting algorithms_Gnome sort


C++: #include <algorithm>
#include <iterator>
#include <iostream>

template<typename RandomAccessIterator>
void gnome_sort(RandomAccessIterator begin, RandomAccessIterator end) {
  auto i = begin + 1;
  auto j = begin + 2;
 
  while (i < end) {
    if (!(*i < *(i - 1))) {
      i = j;
      ++j;
    } else {
      std::iter_swap(i - 1, i);
      --i;
      if (i == begin) {
        i = j;
        ++j;
      }
    }
  }
}

int main() {
  int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
  gnome_sort(std::begin(a), std::end(a));
  copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
  std::cout << "\n";
}
Task: Sorting algorithms_Gnome sort





Java: public static void gnomeSort(int[] a)
{
  int i=1;
  int j=2;
 
  while(i < a.length) {
    if ( a[i-1] <= a[i] ) {
      i = j; j++;
    } else {
      int tmp = a[i-1];
      a[i-1] = a[i];
      a[i--] = tmp;
      i = (i==0) ? j++ : i;
    }
  }
}
Task: Sorting algorithms_Gnome sort







Python: >>> def gnomesort(a):
	i,j,size = 1,2,len(a)
	while i < size:
		if a[i-1] <= a[i]:
			i,j = j, j+1
		else:
			a[i-1],a[i] = a[i],a[i-1]
			i -= 1
			if i == 0:
				i,j = j, j+1
	return a

>>> gnomesort([3,4,2,5,1,6])
[1, 2, 3, 4, 5, 6]
>>>
Task: Sorting algorithms_Gnome sort








VB: Private Function gnomeSort(s As Variant) As Variant
    Dim i As Integer: i = 1
    Dim j As Integer: j = 2
    Dim tmp As Integer
    Do While i < UBound(s)
        If s(i) <= s(i + 1) Then
            i = j
            j = j + 1
        Else
            tmp = s(i)
            s(i) = s(i + 1)
            s(i + 1) = tmp
            i = i - 1
            If i = 0 Then
                i = j
                j = j + 1
            End If
        End If
    Loop
    gnomeSort = s
End Function
 
Public Sub main()
    Debug.Print Join(gnomeSort([{5, 3, 1, 7, 4, 1, 1, 20}]), ", ")
End Sub
Task: Sorting algorithms_Gnome sort




Go: package main

import "fmt"

func main() {
    a := []int{170, 45, 75, -90, -802, 24, 2, 66}
    fmt.Println("before:", a)
    gnomeSort(a)
    fmt.Println("after: ", a)
}

func gnomeSort(a []int) {
    for i, j := 1, 2; i < len(a); {
        if a[i-1] > a[i] {
            a[i-1], a[i] = a[i], a[i-1]
            i--
            if i > 0 {
                continue
            }
        }
        i = j
        j++
    }
}
Task: Sorting algorithms_Gnome sort



C#:         public static void gnomeSort(int[] anArray)
        {
            int first = 1;
            int second = 2;

            while (first < anArray.Length)
            {
                if (anArray[first - 1] <= anArray[first])
                {
                    first = second;
                    second++;
                }
                else
                {
                    int tmp = anArray[first - 1];
                    anArray[first - 1] = anArray[first];
                    anArray[first] = tmp;
                    first -= 1;
                    if (first == 0)
                    {
                        first = 1;
                        second = 2;
                    }
                }
                
            }
        }








Fortran: program example
 
  implicit none
 
  integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)

  call Gnomesort(array)
  write(*,*) array

contains

subroutine Gnomesort(a)

  integer, intent(in out) :: a(:)
  integer :: i, j, temp

  i = 2
  j = 3
  do while (i <= size(a))
    if (a(i-1) <= a(i)) then
      i = j
      j = j + 1
    else
      temp = a(i-1)
      a(i-1) = a(i)
      a(i) = temp
      i = i -  1
      if (i == 1) then
        i = j
        j = j + 1
      end if
    end if
  end do

end subroutine Gnomesort

Optimized Version

      SUBROUTINE OPTIMIZEDGNOMESORT(A)     
      IMPLICIT NONE



      REAL , DIMENSION(0:)  ::  A
      INTENT (INOUT) A



      INTEGER  ::  posy

      DO posy = 1 , UBOUND(A , 1)       
         CALL GNOMESORT(A , posy)
      END DO
      RETURN
      CONTAINS
 
      SUBROUTINE GNOMESORT(A , Upperbound)
      IMPLICIT NONE



      INTEGER  ::  Upperbound
      REAL , DIMENSION(0:)  ::  A
      INTENT (IN) Upperbound
      INTENT (INOUT) A



      LOGICAL  ::  eval
      INTEGER  ::  posy
      REAL  ::  t

      eval = .FALSE.
      posy = Upperbound
      eval = (posy>0) .AND. (A(posy - 1)>A(posy))

      DO WHILE ( eval )
         t = A(posy)
         A(posy) = A(posy - 1)
         A(posy - 1) = t

         posy = posy - 1
         eval = (posy>0)
         IF( eval )THEN     
            eval = (A(posy - 1)>A(posy))
         ELSE
            eval = .FALSE.
         END IF
      END DO
      RETURN
      END SUBROUTINE GNOMESORT
 
      END SUBROUTINE OPTIMIZEDGNOMESORT

  
end program example
Task: Sorting algorithms_Gnome sort


C++: #include <algorithm>
#include <iterator>
#include <iostream>

template<typename RandomAccessIterator>
void gnome_sort(RandomAccessIterator begin, RandomAccessIterator end) {
  auto i = begin + 1;
  auto j = begin + 2;
 
  while (i < end) {
    if (!(*i < *(i - 1))) {
      i = j;
      ++j;
    } else {
      std::iter_swap(i - 1, i);
      --i;
      if (i == begin) {
        i = j;
        ++j;
      }
    }
  }
}

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









Fortran: program example
 
  implicit none
 
  integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)

  call Gnomesort(array)
  write(*,*) array

contains

subroutine Gnomesort(a)

  integer, intent(in out) :: a(:)
  integer :: i, j, temp

  i = 2
  j = 3
  do while (i <= size(a))
    if (a(i-1) <= a(i)) then
      i = j
      j = j + 1
    else
      temp = a(i-1)
      a(i-1) = a(i)
      a(i) = temp
      i = i -  1
      if (i == 1) then
        i = j
        j = j + 1
      end if
    end if
  end do

end subroutine Gnomesort

Optimized Version

      SUBROUTINE OPTIMIZEDGNOMESORT(A)     
      IMPLICIT NONE



      REAL , DIMENSION(0:)  ::  A
      INTENT (INOUT) A



      INTEGER  ::  posy

      DO posy = 1 , UBOUND(A , 1)       
         CALL GNOMESORT(A , posy)
      END DO
      RETURN
      CONTAINS
 
      SUBROUTINE GNOMESORT(A , Upperbound)
      IMPLICIT NONE



      INTEGER  ::  Upperbound
      REAL , DIMENSION(0:)  ::  A
      INTENT (IN) Upperbound
      INTENT (INOUT) A



      LOGICAL  ::  eval
      INTEGER  ::  posy
      REAL  ::  t

      eval = .FALSE.
      posy = Upperbound
      eval = (posy>0) .AND. (A(posy - 1)>A(posy))

      DO WHILE ( eval )
         t = A(posy)
         A(posy) = A(posy - 1)
         A(posy - 1) = t

         posy = posy - 1
         eval = (posy>0)
         IF( eval )THEN     
            eval = (A(posy - 1)>A(posy))
         ELSE
            eval = .FALSE.
         END IF
      END DO
      RETURN
      END SUBROUTINE GNOMESORT
 
      END SUBROUTINE OPTIMIZEDGNOMESORT

  
end program example
Task: Sorting algorithms_Gnome sort

C: void gnome_sort(int *a, int n)
{
  int i=1, j=2, t;
# define swap(i, j) { t = a[i]; a[i] = a[j]; a[j] = t; } 
  while(i < n) {
    if (a[i - 1] > a[i]) {
      swap(i - 1, i);
      if (--i) continue;
    }
    i = j++;
  }
# undef swap
}










Fortran: program example
 
  implicit none
 
  integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)

  call Gnomesort(array)
  write(*,*) array

contains

subroutine Gnomesort(a)

  integer, intent(in out) :: a(:)
  integer :: i, j, temp

  i = 2
  j = 3
  do while (i <= size(a))
    if (a(i-1) <= a(i)) then
      i = j
      j = j + 1
    else
      temp = a(i-1)
      a(i-1) = a(i)
      a(i) = temp
      i = i -  1
      if (i == 1) then
        i = j
        j = j + 1
      end if
    end if
  end do

end subroutine Gnomesort

Optimized Version

      SUBROUTINE OPTIMIZEDGNOMESORT(A)     
      IMPLICIT NONE



      REAL , DIMENSION(0:)  ::  A
      INTENT (INOUT) A



      INTEGER  ::  posy

      DO posy = 1 , UBOUND(A , 1)       
         CALL GNOMESORT(A , posy)
      END DO
      RETURN
      CONTAINS
 
      SUBROUTINE GNOMESORT(A , Upperbound)
      IMPLICIT NONE



      INTEGER  ::  Upperbound
      REAL , DIMENSION(0:)  ::  A
      INTENT (IN) Upperbound
      INTENT (INOUT) A



      LOGICAL  ::  eval
      INTEGER  ::  posy
      REAL  ::  t

      eval = .FALSE.
      posy = Upperbound
      eval = (posy>0) .AND. (A(posy - 1)>A(posy))

      DO WHILE ( eval )
         t = A(posy)
         A(posy) = A(posy - 1)
         A(posy - 1) = t

         posy = posy - 1
         eval = (posy>0)
         IF( eval )THEN     
            eval = (A(posy - 1)>A(posy))
         ELSE
            eval = .FALSE.
         END IF
      END DO
      RETURN
      END SUBROUTINE GNOMESORT
 
      END SUBROUTINE OPTIMIZEDGNOMESORT

  
end program example
Task: Sorting algorithms_Gnome sort





Java: public static void gnomeSort(int[] a)
{
  int i=1;
  int j=2;
 
  while(i < a.length) {
    if ( a[i-1] <= a[i] ) {
      i = j; j++;
    } else {
      int tmp = a[i-1];
      a[i-1] = a[i];
      a[i--] = tmp;
      i = (i==0) ? j++ : i;
    }
  }
}






Fortran: program example
 
  implicit none
 
  integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)

  call Gnomesort(array)
  write(*,*) array

contains

subroutine Gnomesort(a)

  integer, intent(in out) :: a(:)
  integer :: i, j, temp

  i = 2
  j = 3
  do while (i <= size(a))
    if (a(i-1) <= a(i)) then
      i = j
      j = j + 1
    else
      temp = a(i-1)
      a(i-1) = a(i)
      a(i) = temp
      i = i -  1
      if (i == 1) then
        i = j
        j = j + 1
      end if
    end if
  end do

end subroutine Gnomesort

Optimized Version

      SUBROUTINE OPTIMIZEDGNOMESORT(A)     
      IMPLICIT NONE



      REAL , DIMENSION(0:)  ::  A
      INTENT (INOUT) A



      INTEGER  ::  posy

      DO posy = 1 , UBOUND(A , 1)       
         CALL GNOMESORT(A , posy)
      END DO
      RETURN
      CONTAINS
 
      SUBROUTINE GNOMESORT(A , Upperbound)
      IMPLICIT NONE



      INTEGER  ::  Upperbound
      REAL , DIMENSION(0:)  ::  A
      INTENT (IN) Upperbound
      INTENT (INOUT) A



      LOGICAL  ::  eval
      INTEGER  ::  posy
      REAL  ::  t

      eval = .FALSE.
      posy = Upperbound
      eval = (posy>0) .AND. (A(posy - 1)>A(posy))

      DO WHILE ( eval )
         t = A(posy)
         A(posy) = A(posy - 1)
         A(posy - 1) = t

         posy = posy - 1
         eval = (posy>0)
         IF( eval )THEN     
            eval = (A(posy - 1)>A(posy))
         ELSE
            eval = .FALSE.
         END IF
      END DO
      RETURN
      END SUBROUTINE GNOMESORT
 
      END SUBROUTINE OPTIMIZEDGNOMESORT

  
end program example
Task: Sorting algorithms_Gnome sort







Python: >>> def gnomesort(a):
	i,j,size = 1,2,len(a)
	while i < size:
		if a[i-1] <= a[i]:
			i,j = j, j+1
		else:
			a[i-1],a[i] = a[i],a[i-1]
			i -= 1
			if i == 0:
				i,j = j, j+1
	return a

>>> gnomesort([3,4,2,5,1,6])
[1, 2, 3, 4, 5, 6]
>>>




Fortran: program example
 
  implicit none
 
  integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)

  call Gnomesort(array)
  write(*,*) array

contains

subroutine Gnomesort(a)

  integer, intent(in out) :: a(:)
  integer :: i, j, temp

  i = 2
  j = 3
  do while (i <= size(a))
    if (a(i-1) <= a(i)) then
      i = j
      j = j + 1
    else
      temp = a(i-1)
      a(i-1) = a(i)
      a(i) = temp
      i = i -  1
      if (i == 1) then
        i = j
        j = j + 1
      end if
    end if
  end do

end subroutine Gnomesort

Optimized Version

      SUBROUTINE OPTIMIZEDGNOMESORT(A)     
      IMPLICIT NONE



      REAL , DIMENSION(0:)  ::  A
      INTENT (INOUT) A



      INTEGER  ::  posy

      DO posy = 1 , UBOUND(A , 1)       
         CALL GNOMESORT(A , posy)
      END DO
      RETURN
      CONTAINS
 
      SUBROUTINE GNOMESORT(A , Upperbound)
      IMPLICIT NONE



      INTEGER  ::  Upperbound
      REAL , DIMENSION(0:)  ::  A
      INTENT (IN) Upperbound
      INTENT (INOUT) A



      LOGICAL  ::  eval
      INTEGER  ::  posy
      REAL  ::  t

      eval = .FALSE.
      posy = Upperbound
      eval = (posy>0) .AND. (A(posy - 1)>A(posy))

      DO WHILE ( eval )
         t = A(posy)
         A(posy) = A(posy - 1)
         A(posy - 1) = t

         posy = posy - 1
         eval = (posy>0)
         IF( eval )THEN     
            eval = (A(posy - 1)>A(posy))
         ELSE
            eval = .FALSE.
         END IF
      END DO
      RETURN
      END SUBROUTINE GNOMESORT
 
      END SUBROUTINE OPTIMIZEDGNOMESORT

  
end program example
Task: Sorting algorithms_Gnome sort








VB: Private Function gnomeSort(s As Variant) As Variant
    Dim i As Integer: i = 1
    Dim j As Integer: j = 2
    Dim tmp As Integer
    Do While i < UBound(s)
        If s(i) <= s(i + 1) Then
            i = j
            j = j + 1
        Else
            tmp = s(i)
            s(i) = s(i + 1)
            s(i + 1) = tmp
            i = i - 1
            If i = 0 Then
                i = j
                j = j + 1
            End If
        End If
    Loop
    gnomeSort = s
End Function
 
Public Sub main()
    Debug.Print Join(gnomeSort([{5, 3, 1, 7, 4, 1, 1, 20}]), ", ")
End Sub



Fortran: program example
 
  implicit none
 
  integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)

  call Gnomesort(array)
  write(*,*) array

contains

subroutine Gnomesort(a)

  integer, intent(in out) :: a(:)
  integer :: i, j, temp

  i = 2
  j = 3
  do while (i <= size(a))
    if (a(i-1) <= a(i)) then
      i = j
      j = j + 1
    else
      temp = a(i-1)
      a(i-1) = a(i)
      a(i) = temp
      i = i -  1
      if (i == 1) then
        i = j
        j = j + 1
      end if
    end if
  end do

end subroutine Gnomesort

Optimized Version

      SUBROUTINE OPTIMIZEDGNOMESORT(A)     
      IMPLICIT NONE



      REAL , DIMENSION(0:)  ::  A
      INTENT (INOUT) A



      INTEGER  ::  posy

      DO posy = 1 , UBOUND(A , 1)       
         CALL GNOMESORT(A , posy)
      END DO
      RETURN
      CONTAINS
 
      SUBROUTINE GNOMESORT(A , Upperbound)
      IMPLICIT NONE



      INTEGER  ::  Upperbound
      REAL , DIMENSION(0:)  ::  A
      INTENT (IN) Upperbound
      INTENT (INOUT) A



      LOGICAL  ::  eval
      INTEGER  ::  posy
      REAL  ::  t

      eval = .FALSE.
      posy = Upperbound
      eval = (posy>0) .AND. (A(posy - 1)>A(posy))

      DO WHILE ( eval )
         t = A(posy)
         A(posy) = A(posy - 1)
         A(posy - 1) = t

         posy = posy - 1
         eval = (posy>0)
         IF( eval )THEN     
            eval = (A(posy - 1)>A(posy))
         ELSE
            eval = .FALSE.
         END IF
      END DO
      RETURN
      END SUBROUTINE GNOMESORT
 
      END SUBROUTINE OPTIMIZEDGNOMESORT

  
end program example
Task: Sorting algorithms_Gnome sort






PHP: function gnomeSort($arr){
	$i = 1;
	$j = 2;
	while($i < count($arr)){
		if ($arr[$i-1] <= $arr[$i]){
			$i = $j;
			$j++;
		}else{
			list($arr[$i],$arr[$i-1]) = array($arr[$i-1],$arr[$i]);
			$i--;
			if($i == 0){
				$i = $j;
				$j++;
			}
		}
	}
	return $arr;
}
$arr = array(3,1,6,2,9,4,7,8,5);
echo implode(',',gnomeSort($arr));





Fortran: program example
 
  implicit none
 
  integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)

  call Gnomesort(array)
  write(*,*) array

contains

subroutine Gnomesort(a)

  integer, intent(in out) :: a(:)
  integer :: i, j, temp

  i = 2
  j = 3
  do while (i <= size(a))
    if (a(i-1) <= a(i)) then
      i = j
      j = j + 1
    else
      temp = a(i-1)
      a(i-1) = a(i)
      a(i) = temp
      i = i -  1
      if (i == 1) then
        i = j
        j = j + 1
      end if
    end if
  end do

end subroutine Gnomesort

Optimized Version

      SUBROUTINE OPTIMIZEDGNOMESORT(A)     
      IMPLICIT NONE



      REAL , DIMENSION(0:)  ::  A
      INTENT (INOUT) A



      INTEGER  ::  posy

      DO posy = 1 , UBOUND(A , 1)       
         CALL GNOMESORT(A , posy)
      END DO
      RETURN
      CONTAINS
 
      SUBROUTINE GNOMESORT(A , Upperbound)
      IMPLICIT NONE



      INTEGER  ::  Upperbound
      REAL , DIMENSION(0:)  ::  A
      INTENT (IN) Upperbound
      INTENT (INOUT) A



      LOGICAL  ::  eval
      INTEGER  ::  posy
      REAL  ::  t

      eval = .FALSE.
      posy = Upperbound
      eval = (posy>0) .AND. (A(posy - 1)>A(posy))

      DO WHILE ( eval )
         t = A(posy)
         A(posy) = A(posy - 1)
         A(posy - 1) = t

         posy = posy - 1
         eval = (posy>0)
         IF( eval )THEN     
            eval = (A(posy - 1)>A(posy))
         ELSE
            eval = .FALSE.
         END IF
      END DO
      RETURN
      END SUBROUTINE GNOMESORT
 
      END SUBROUTINE OPTIMIZEDGNOMESORT

  
end program example
Task: Sorting algorithms_Gnome sort

C: void gnome_sort(int *a, int n)
{
  int i=1, j=2, t;
# define swap(i, j) { t = a[i]; a[i] = a[j]; a[j] = t; } 
  while(i < n) {
    if (a[i - 1] > a[i]) {
      swap(i - 1, i);
      if (--i) continue;
    }
    i = j++;
  }
# undef swap
}









COBOL:        C-SORT SECTION.
       C-000.
           DISPLAY "SORT STARTING".

           SET WB-IX-1 TO 2.
           MOVE 1 TO WC-NEXT-POSN.

           PERFORM E-GNOME UNTIL WC-NEXT-POSN > WC-SIZE.

           DISPLAY "SORT FINISHED".

       C-999.
           EXIT.

       E-GNOME SECTION.
       E-000.
           IF WB-ENTRY(WB-IX-1 - 1) NOT > WB-ENTRY(WB-IX-1)
              ADD 1       TO WC-NEXT-POSN
              SET WB-IX-1 TO WC-NEXT-POSN
           ELSE
              MOVE WB-ENTRY(WB-IX-1 - 1) TO WC-TEMP
              MOVE WB-ENTRY(WB-IX-1)     TO WB-ENTRY(WB-IX-1 - 1)
              MOVE WC-TEMP               TO WB-ENTRY(WB-IX-1)
              SET WB-IX-1                DOWN BY 1
              IF WB-IX-1 = 1
                 ADD 1       TO WC-NEXT-POSN
                 SET WB-IX-1 TO WC-NEXT-POSN.

       E-999.
           EXIT.
Task: Sorting algorithms_Gnome sort



C#:         public static void gnomeSort(int[] anArray)
        {
            int first = 1;
            int second = 2;

            while (first < anArray.Length)
            {
                if (anArray[first - 1] <= anArray[first])
                {
                    first = second;
                    second++;
                }
                else
                {
                    int tmp = anArray[first - 1];
                    anArray[first - 1] = anArray[first];
                    anArray[first] = tmp;
                    first -= 1;
                    if (first == 0)
                    {
                        first = 1;
                        second = 2;
                    }
                }
                
            }
        }







COBOL:        C-SORT SECTION.
       C-000.
           DISPLAY "SORT STARTING".

           SET WB-IX-1 TO 2.
           MOVE 1 TO WC-NEXT-POSN.

           PERFORM E-GNOME UNTIL WC-NEXT-POSN > WC-SIZE.

           DISPLAY "SORT FINISHED".

       C-999.
           EXIT.

       E-GNOME SECTION.
       E-000.
           IF WB-ENTRY(WB-IX-1 - 1) NOT > WB-ENTRY(WB-IX-1)
              ADD 1       TO WC-NEXT-POSN
              SET WB-IX-1 TO WC-NEXT-POSN
           ELSE
              MOVE WB-ENTRY(WB-IX-1 - 1) TO WC-TEMP
              MOVE WB-ENTRY(WB-IX-1)     TO WB-ENTRY(WB-IX-1 - 1)
              MOVE WC-TEMP               TO WB-ENTRY(WB-IX-1)
              SET WB-IX-1                DOWN BY 1
              IF WB-IX-1 = 1
                 ADD 1       TO WC-NEXT-POSN
                 SET WB-IX-1 TO WC-NEXT-POSN.

       E-999.
           EXIT.
Task: Sorting algorithms_Gnome sort


C++: #include <algorithm>
#include <iterator>
#include <iostream>

template<typename RandomAccessIterator>
void gnome_sort(RandomAccessIterator begin, RandomAccessIterator end) {
  auto i = begin + 1;
  auto j = begin + 2;
 
  while (i < end) {
    if (!(*i < *(i - 1))) {
      i = j;
      ++j;
    } else {
      std::iter_swap(i - 1, i);
      --i;
      if (i == begin) {
        i = j;
        ++j;
      }
    }
  }
}

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








COBOL:        C-SORT SECTION.
       C-000.
           DISPLAY "SORT STARTING".

           SET WB-IX-1 TO 2.
           MOVE 1 TO WC-NEXT-POSN.

           PERFORM E-GNOME UNTIL WC-NEXT-POSN > WC-SIZE.

           DISPLAY "SORT FINISHED".

       C-999.
           EXIT.

       E-GNOME SECTION.
       E-000.
           IF WB-ENTRY(WB-IX-1 - 1) NOT > WB-ENTRY(WB-IX-1)
              ADD 1       TO WC-NEXT-POSN
              SET WB-IX-1 TO WC-NEXT-POSN
           ELSE
              MOVE WB-ENTRY(WB-IX-1 - 1) TO WC-TEMP
              MOVE WB-ENTRY(WB-IX-1)     TO WB-ENTRY(WB-IX-1 - 1)
              MOVE WC-TEMP               TO WB-ENTRY(WB-IX-1)
              SET WB-IX-1                DOWN BY 1
              IF WB-IX-1 = 1
                 ADD 1       TO WC-NEXT-POSN
                 SET WB-IX-1 TO WC-NEXT-POSN.

       E-999.
           EXIT.
Task: Sorting algorithms_Gnome sort





Java: public static void gnomeSort(int[] a)
{
  int i=1;
  int j=2;
 
  while(i < a.length) {
    if ( a[i-1] <= a[i] ) {
      i = j; j++;
    } else {
      int tmp = a[i-1];
      a[i-1] = a[i];
      a[i--] = tmp;
      i = (i==0) ? j++ : i;
    }
  }
}





COBOL:        C-SORT SECTION.
       C-000.
           DISPLAY "SORT STARTING".

           SET WB-IX-1 TO 2.
           MOVE 1 TO WC-NEXT-POSN.

           PERFORM E-GNOME UNTIL WC-NEXT-POSN > WC-SIZE.

           DISPLAY "SORT FINISHED".

       C-999.
           EXIT.

       E-GNOME SECTION.
       E-000.
           IF WB-ENTRY(WB-IX-1 - 1) NOT > WB-ENTRY(WB-IX-1)
              ADD 1       TO WC-NEXT-POSN
              SET WB-IX-1 TO WC-NEXT-POSN
           ELSE
              MOVE WB-ENTRY(WB-IX-1 - 1) TO WC-TEMP
              MOVE WB-ENTRY(WB-IX-1)     TO WB-ENTRY(WB-IX-1 - 1)
              MOVE WC-TEMP               TO WB-ENTRY(WB-IX-1)
              SET WB-IX-1                DOWN BY 1
              IF WB-IX-1 = 1
                 ADD 1       TO WC-NEXT-POSN
                 SET WB-IX-1 TO WC-NEXT-POSN.

       E-999.
           EXIT.
Task: Sorting algorithms_Gnome sort







Python: >>> def gnomesort(a):
	i,j,size = 1,2,len(a)
	while i < size:
		if a[i-1] <= a[i]:
			i,j = j, j+1
		else:
			a[i-1],a[i] = a[i],a[i-1]
			i -= 1
			if i == 0:
				i,j = j, j+1
	return a

>>> gnomesort([3,4,2,5,1,6])
[1, 2, 3, 4, 5, 6]
>>>



COBOL:        C-SORT SECTION.
       C-000.
           DISPLAY "SORT STARTING".

           SET WB-IX-1 TO 2.
           MOVE 1 TO WC-NEXT-POSN.

           PERFORM E-GNOME UNTIL WC-NEXT-POSN > WC-SIZE.

           DISPLAY "SORT FINISHED".

       C-999.
           EXIT.

       E-GNOME SECTION.
       E-000.
           IF WB-ENTRY(WB-IX-1 - 1) NOT > WB-ENTRY(WB-IX-1)
              ADD 1       TO WC-NEXT-POSN
              SET WB-IX-1 TO WC-NEXT-POSN
           ELSE
              MOVE WB-ENTRY(WB-IX-1 - 1) TO WC-TEMP
              MOVE WB-ENTRY(WB-IX-1)     TO WB-ENTRY(WB-IX-1 - 1)
              MOVE WC-TEMP               TO WB-ENTRY(WB-IX-1)
              SET WB-IX-1                DOWN BY 1
              IF WB-IX-1 = 1
                 ADD 1       TO WC-NEXT-POSN
                 SET WB-IX-1 TO WC-NEXT-POSN.

       E-999.
           EXIT.