WeixiangYan/CodeTransOcean huggingface dataset:
Task: Show ASCII table





Java: module test
    {
    @Inject Console console;
    void run()
        {
        for (Int offset : 0..<16)
            {
            for (Int ascii = 32+offset; ascii < 128; ascii += 16)
                {
                console.print($|{ascii.toString().rightJustify(3)}/\
                               |{ascii.toByte().toByteArray()}: \
                               |{new Char(ascii).quoted().leftJustify(5)}
                              , suppressNewline=True);
                }
            console.print();
            }
        }
    }
Task: Show ASCII table







Python: for i in range(16):
    for j in range(32+i, 127+1, 16):
        if j == 32:
            k = 'Spc'
        elif j == 127:
            k = 'Del'
        else:
            k = chr(j)
        print("%3d : %-3s" % (j,k), end="")
    print()
Task: Show ASCII table








VB: for i = 32 to 47
	for j = i to i + 80 step 16
		begin case
			case j = 32
				s$ = "Spc"
			case j = 127
				s$ = "Del"
			else
				s$ = chr(j)
		end case
		print rjust("  "+string(j),5); ": "; ljust(s$,3);
	next j
	print
next i
Task: Show ASCII table




Go: package main

import "fmt"

func main() {
    for i := 0; i < 16; i++ {
        for j := 32 + i; j < 128; j += 16 {
            k := string(j)
            switch j {
            case 32:
                k = "Spc"
            case 127:
                k = "Del"
            }
            fmt.Printf("%3d : %-3s   ", j, k)
        }
        fmt.Println()
    }
}
Task: Show ASCII table



C#: using static System.Console;
using static System.Linq.Enumerable;

public class Program
{
    static void Main()
    {
        for (int start = 32; start + 16 * 5 < 128; start++) {
            WriteLine(string.Concat(Range(0, 6).Select(i => $"{start+16*i, 3} : {Text(start+16*i), -6}")));
        }

        string Text(int index) => index == 32 ? "Sp" : index == 127 ? "Del" : (char)index + "";
    }
}








Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table


C++: #include <string> 
#include <iomanip> 
#include <iostream>


#define HEIGHT        16
#define WIDTH         6
#define ASCII_START   32
#define ASCII_END     128

#define SPACE         32 
#define DELETE        127

std::string displayAscii(int ascii) {
  switch(ascii) {
    case SPACE:
      return "Spc";
    case DELETE:
      return "Del";
    default:
      return std::string(1,char(ascii));
  }
}

int main(void) {

  for(int row = 0; row < HEIGHT; ++row) {
    for(int col = 0; col < WIDTH; ++col) {
      int ascii = ASCII_START + row + col*HEIGHT; 
      std::cout << std::right << std::setw(3) << ascii << " : " \
        << std::left << std::setw(6) << displayAscii(ascii);   
    }
    std::cout << std::endl;
  }
}









Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table

C: #include <stdio.h>

int main() {
    int i, j;
    char k[4];
    for (i = 0; i < 16; ++i) {
        for (j = 32 + i; j < 128; j += 16) {
            switch (j) {
                default:  sprintf(k, "%c", j); break;
                case 32:  sprintf(k, "Spc"); break;
                case 127: sprintf(k, "Del"); break;
            }
            printf("%3d : %-3s   ", j, k);
        }
        printf("\n");
    }
    return 0;
}










Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table




Go: package main

import "fmt"

func main() {
    for i := 0; i < 16; i++ {
        for j := 32 + i; j < 128; j += 16 {
            k := string(j)
            switch j {
            case 32:
                k = "Spc"
            case 127:
                k = "Del"
            }
            fmt.Printf("%3d : %-3s   ", j, k)
        }
        fmt.Println()
    }
}







Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table





Java: module test
    {
    @Inject Console console;
    void run()
        {
        for (Int offset : 0..<16)
            {
            for (Int ascii = 32+offset; ascii < 128; ascii += 16)
                {
                console.print($|{ascii.toString().rightJustify(3)}/\
                               |{ascii.toByte().toByteArray()}: \
                               |{new Char(ascii).quoted().leftJustify(5)}
                              , suppressNewline=True);
                }
            console.print();
            }
        }
    }






Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table







Python: for i in range(16):
    for j in range(32+i, 127+1, 16):
        if j == 32:
            k = 'Spc'
        elif j == 127:
            k = 'Del'
        else:
            k = chr(j)
        print("%3d : %-3s" % (j,k), end="")
    print()




Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table








VB: for i = 32 to 47
	for j = i to i + 80 step 16
		begin case
			case j = 32
				s$ = "Spc"
			case j = 127
				s$ = "Del"
			else
				s$ = chr(j)
		end case
		print rjust("  "+string(j),5); ": "; ljust(s$,3);
	next j
	print
next i



Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table






PHP: <?php

echo '+' . str_repeat('----------+', 6), PHP_EOL;
for ($j = 0 ; $j < 16 ; $j++) {
    for ($i = 0 ; $i < 6 ; $i++) {
        $val = 32 + $i * 16 + $j;
        switch ($val) {
            case  32: $chr = 'Spc';      break;
            case 127: $chr = 'Del';      break;
            default:  $chr = chr($val) ; break;
        }
        echo sprintf('| %3d: %3s ', $val, $chr);
    }
    echo '|', PHP_EOL;
}
echo '+' . str_repeat('----------+', 6), PHP_EOL;





Fortran:        PROGRAM ASCTBL  
       IMPLICIT NONE
       INTEGER I, J
       CHARACTER*3 H

  10   FORMAT (I3, ':', A3, '   ', $)
  20   FORMAT ()
       DO J = 0, 15, +1
         DO I = 32+J, 127, +16
           IF (I > 32 .AND. I < 127) THEN
             H = ' ' // ACHAR(I) // ' '
           ELSE IF (I .EQ. 32) THEN
             H = 'Spc'
           ELSE IF (I .EQ. 127) THEN
             H = 'Del'
           ELSE
             STOP 'bad value of i'
           END IF
           PRINT 10, I, H
         END DO
         PRINT 20
       END DO

       END
Task: Show ASCII table

C: #include <stdio.h>

int main() {
    int i, j;
    char k[4];
    for (i = 0; i < 16; ++i) {
        for (j = 32 + i; j < 128; j += 16) {
            switch (j) {
                default:  sprintf(k, "%c", j); break;
                case 32:  sprintf(k, "Spc"); break;
                case 127: sprintf(k, "Del"); break;
            }
            printf("%3d : %-3s   ", j, k);
        }
        printf("\n");
    }
    return 0;
}









COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table



C#: using static System.Console;
using static System.Linq.Enumerable;

public class Program
{
    static void Main()
    {
        for (int start = 32; start + 16 * 5 < 128; start++) {
            WriteLine(string.Concat(Range(0, 6).Select(i => $"{start+16*i, 3} : {Text(start+16*i), -6}")));
        }

        string Text(int index) => index == 32 ? "Sp" : index == 127 ? "Del" : (char)index + "";
    }
}







COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table


C++: #include <string> 
#include <iomanip> 
#include <iostream>


#define HEIGHT        16
#define WIDTH         6
#define ASCII_START   32
#define ASCII_END     128

#define SPACE         32 
#define DELETE        127

std::string displayAscii(int ascii) {
  switch(ascii) {
    case SPACE:
      return "Spc";
    case DELETE:
      return "Del";
    default:
      return std::string(1,char(ascii));
  }
}

int main(void) {

  for(int row = 0; row < HEIGHT; ++row) {
    for(int col = 0; col < WIDTH; ++col) {
      int ascii = ASCII_START + row + col*HEIGHT; 
      std::cout << std::right << std::setw(3) << ascii << " : " \
        << std::left << std::setw(6) << displayAscii(ascii);   
    }
    std::cout << std::endl;
  }
}








COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table





Java: module test
    {
    @Inject Console console;
    void run()
        {
        for (Int offset : 0..<16)
            {
            for (Int ascii = 32+offset; ascii < 128; ascii += 16)
                {
                console.print($|{ascii.toString().rightJustify(3)}/\
                               |{ascii.toByte().toByteArray()}: \
                               |{new Char(ascii).quoted().leftJustify(5)}
                              , suppressNewline=True);
                }
            console.print();
            }
        }
    }





COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table







Python: for i in range(16):
    for j in range(32+i, 127+1, 16):
        if j == 32:
            k = 'Spc'
        elif j == 127:
            k = 'Del'
        else:
            k = chr(j)
        print("%3d : %-3s" % (j,k), end="")
    print()



COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table








VB: for i = 32 to 47
	for j = i to i + 80 step 16
		begin case
			case j = 32
				s$ = "Spc"
			case j = 127
				s$ = "Del"
			else
				s$ = chr(j)
		end case
		print rjust("  "+string(j),5); ": "; ljust(s$,3);
	next j
	print
next i


COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table




Go: package main

import "fmt"

func main() {
    for i := 0; i < 16; i++ {
        for j := 32 + i; j < 128; j += 16 {
            k := string(j)
            switch j {
            case 32:
                k = "Spc"
            case 127:
                k = "Del"
            }
            fmt.Printf("%3d : %-3s   ", j, k)
        }
        fmt.Println()
    }
}






COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table






PHP: <?php

echo '+' . str_repeat('----------+', 6), PHP_EOL;
for ($j = 0 ; $j < 16 ; $j++) {
    for ($i = 0 ; $i < 6 ; $i++) {
        $val = 32 + $i * 16 + $j;
        switch ($val) {
            case  32: $chr = 'Spc';      break;
            case 127: $chr = 'Del';      break;
            default:  $chr = chr($val) ; break;
        }
        echo sprintf('| %3d: %3s ', $val, $chr);
    }
    echo '|', PHP_EOL;
}
echo '+' . str_repeat('----------+', 6), PHP_EOL;
Task: Show ASCII table






PHP: <?php

echo '+' . str_repeat('----------+', 6), PHP_EOL;
for ($j = 0 ; $j < 16 ; $j++) {
    for ($i = 0 ; $i < 6 ; $i++) {
        $val = 32 + $i * 16 + $j;
        switch ($val) {
            case  32: $chr = 'Spc';      break;
            case 127: $chr = 'Del';      break;
            default:  $chr = chr($val) ; break;
        }
        echo sprintf('| %3d: %3s ', $val, $chr);
    }
    echo '|', PHP_EOL;
}
echo '+' . str_repeat('----------+', 6), PHP_EOL;






Ada: with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Ascii_Table is
   N : Integer;
begin
   for R in 0 .. 15 loop
      for C in 0 .. 5 loop
         N := 32 + 16 * C + R;
         Put (N, 3);
         Put (" : ");
         case N is
            when 32 =>
               Put ("Spc  ");
            when 127 =>
               Put ("Del  ");
            when others =>
               Put (Character'Val (N) & "    ");
         end case;
      end loop;
      New_Line;
   end loop;
end Ascii_Table;
Task: Show ASCII table






PHP: <?php

echo '+' . str_repeat('----------+', 6), PHP_EOL;
for ($j = 0 ; $j < 16 ; $j++) {
    for ($i = 0 ; $i < 6 ; $i++) {
        $val = 32 + $i * 16 + $j;
        switch ($val) {
            case  32: $chr = 'Spc';      break;
            case 127: $chr = 'Del';      break;
            default:  $chr = chr($val) ; break;
        }
        echo sprintf('| %3d: %3s ', $val, $chr);
    }
    echo '|', PHP_EOL;
}
echo '+' . str_repeat('----------+', 6), PHP_EOL;




COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. CHARSET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHARCODE PIC 9(3) VALUE 32.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM UNTIL CHARCODE=128
DISPLAY FUNCTION CONCATENATE(
    FUNCTION CONCATENATE(
        CHARCODE,
        " : "
    ),
    FUNCTION CONCATENATE(
        FUNCTION CHAR(CHARCODE),
        "   "
    )
)
WITH NO ADVANCING
ADD 1 TO CHARCODE
END-PERFORM.
END PROGRAM CHARSET.
Task: Show ASCII table








VB: Public Sub ascii()
    Dim s As String, i As Integer, j As Integer
    For i = 0 To 15
        For j = 32 + i To 127 Step 16
            Select Case j
                Case 32: s = "Spc"
                Case 127: s = "Del"
                Case Else: s = Chr(j)
            End Select
            Debug.Print Tab(10 * (j - 32 - i) / 16); Spc(3 - Len(CStr(j))); j & ": " & s;
        Next j
        Debug.Print vbCrLf
    Next i
End Sub
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;
                    }
                }
                
            }
        }









Ada: generic
   type Element_Type is private;
   type Index is (<>);
   type Collection is array(Index) of Element_Type;
   with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);
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
}











Ada: generic
   type Element_Type is private;
   type Index is (<>);
   type Collection is array(Index) of Element_Type;
   with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);
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";
}










Ada: generic
   type Element_Type is private;
   type Index is (<>);
   type Collection is array(Index) of Element_Type;
   with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);
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++
    }
}








Ada: generic
   type Element_Type is private;
   type Index is (<>);
   type Collection is array(Index) of Element_Type;
   with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);
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;
    }
  }
}







Ada: generic
   type Element_Type is private;
   type Index is (<>);
   type Collection is array(Index) of Element_Type;
   with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);
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]
>>>





Ada: generic
   type Element_Type is private;
   type Index is (<>);
   type Collection is array(Index) of Element_Type;
   with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);
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




Ada: generic
   type Element_Type is private;
   type Index is (<>);
   type Collection is array(Index) of Element_Type;
   with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);
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
}