WeixiangYan/CodeTransOcean huggingface dataset:
Task: Loop over multiple arrays simultaneously



C#: class Program
{
    static void Main(string[] args)
    {
        char[] a = { 'a', 'b', 'c' };
        char[] b = { 'A', 'B', 'C' };
        int[] c = { 1, 2, 3 };
        int min = Math.Min(a.Length, b.Length);
        min = Math.Min(min, c.Length);
        for (int i = 0; i < min; i++)
            Console.WriteLine("{0}{1}{2}", a[i], b[i], c[i]);
    }             
}







COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-Over-Multiple-Tables.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A VALUE "abc".
           03  A-Vals PIC X OCCURS 3 TIMES.

       01  B VALUE "ABC".
           03  B-Vals PIC X OCCURS 3 TIMES.

       01  C VALUE "123".
           03  C-Vals PIC 9 OCCURS 3 TIMES.

       01  I PIC 9.

       PROCEDURE DIVISION.
           PERFORM VARYING I FROM 1 BY 1 UNTIL 3 < I
               DISPLAY A-Vals (I) B-Vals (I) C-Vals (I)
           END-PERFORM

           GOBACK
           .
Task: Loop over multiple arrays simultaneously


C++: #include <iostream>
#include <vector>

int main(int argc, char* argv[])
{
   std::vector<char> ls(3); ls[0] = 'a'; ls[1] = 'b'; ls[2] = 'c';
   std::vector<char> us(3); us[0] = 'A'; us[1] = 'B'; us[2] = 'C';
   std::vector<int> ns(3);  ns[0] = 1;   ns[1] = 2;   ns[2] = 3;

   std::vector<char>::const_iterator lIt = ls.begin();
   std::vector<char>::const_iterator uIt = us.begin();
   std::vector<int>::const_iterator nIt = ns.begin();
   for(; lIt != ls.end() && uIt != us.end() && nIt != 
ns.end();
       ++lIt, ++uIt, ++nIt)
   {
      std::cout << *lIt << *uIt << *nIt << "\n";
   }
}








COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-Over-Multiple-Tables.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A VALUE "abc".
           03  A-Vals PIC X OCCURS 3 TIMES.

       01  B VALUE "ABC".
           03  B-Vals PIC X OCCURS 3 TIMES.

       01  C VALUE "123".
           03  C-Vals PIC 9 OCCURS 3 TIMES.

       01  I PIC 9.

       PROCEDURE DIVISION.
           PERFORM VARYING I FROM 1 BY 1 UNTIL 3 < I
               DISPLAY A-Vals (I) B-Vals (I) C-Vals (I)
           END-PERFORM

           GOBACK
           .
Task: Loop over multiple arrays simultaneously





Java: module LoopOverMultipleArrays
    {
    @Inject Console console;

    void run()
        {
        Char[]   chars   = ['a', 'b', 'c'];
        String[] strings = ["A", "B", "C"];
        Int[]    ints    = [ 1,   2,   3 ];

        console.print("Using array indexing:");
        for (Int i = 0, Int longest = chars.size.maxOf(strings.size.maxOf(ints.size)); i < longest; ++i)
            {
            console.print($|{i < chars.size   ? chars[i].toString() : ""}\
                           |{i < strings.size ? strings[i]          : ""}\
                           |{i < ints.size    ? ints[i].toString()  : ""}
                         );
            }

        console.print("\nUsing array iterators:");
        val charIter   = chars.iterator();
        val stringIter = strings.iterator();
        val intIter    = ints.iterator();
        while (True)
            {
            StringBuffer buf = new StringBuffer();
            if (Char ch := charIter.next())
                {
                buf.add(ch);
                }
            if (String s := stringIter.next())
                {
                s.appendTo(buf);
                }
            if (Int n := intIter.next())
                {
                n.appendTo(buf);
                }
            if (buf.size == 0)
                {
                break;
                }
            console.print(buf);
            }
        }
    }





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-Over-Multiple-Tables.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A VALUE "abc".
           03  A-Vals PIC X OCCURS 3 TIMES.

       01  B VALUE "ABC".
           03  B-Vals PIC X OCCURS 3 TIMES.

       01  C VALUE "123".
           03  C-Vals PIC 9 OCCURS 3 TIMES.

       01  I PIC 9.

       PROCEDURE DIVISION.
           PERFORM VARYING I FROM 1 BY 1 UNTIL 3 < I
               DISPLAY A-Vals (I) B-Vals (I) C-Vals (I)
           END-PERFORM

           GOBACK
           .
Task: Loop over multiple arrays simultaneously







Python: >>> print ( '\n'.join(''.join(x) for x in 
zip('abc', 'ABC', '123')) )
aA1
bB2
cC3
>>>



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-Over-Multiple-Tables.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A VALUE "abc".
           03  A-Vals PIC X OCCURS 3 TIMES.

       01  B VALUE "ABC".
           03  B-Vals PIC X OCCURS 3 TIMES.

       01  C VALUE "123".
           03  C-Vals PIC 9 OCCURS 3 TIMES.

       01  I PIC 9.

       PROCEDURE DIVISION.
           PERFORM VARYING I FROM 1 BY 1 UNTIL 3 < I
               DISPLAY A-Vals (I) B-Vals (I) C-Vals (I)
           END-PERFORM

           GOBACK
           .
Task: Loop over multiple arrays simultaneously








VB: Module Program
    Sub Main()
        Dim a As Char() = {"a"c, "b"c, "c"c}
        Dim b As Char() = {"A"c, "B"c, "C"c}
        Dim c As Integer() = {1, 2, 3}

        Dim minLength = {a.Length, b.Length, c.Length}.Min()
        For i = 0 To minLength - 1
            Console.WriteLine(a(i) & b(i) & c(i))
        Next

        Console.WriteLine()

        For Each el As (a As Char, b As Char, c As Integer) In a.Zip(b, Function(l, r) (l, r)).Zip(c, Function(x, r) (x.l, x.r, r))
            Console.WriteLine(el.a & el.b & el.c)
        Next
    End Sub
End Module


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-Over-Multiple-Tables.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A VALUE "abc".
           03  A-Vals PIC X OCCURS 3 TIMES.

       01  B VALUE "ABC".
           03  B-Vals PIC X OCCURS 3 TIMES.

       01  C VALUE "123".
           03  C-Vals PIC 9 OCCURS 3 TIMES.

       01  I PIC 9.

       PROCEDURE DIVISION.
           PERFORM VARYING I FROM 1 BY 1 UNTIL 3 < I
               DISPLAY A-Vals (I) B-Vals (I) C-Vals (I)
           END-PERFORM

           GOBACK
           .
Task: Loop over multiple arrays simultaneously




Go: package main

import "fmt"

var a1 = []string{"a", "b", "c"}
var a2 = []byte{'A', 'B', 'C'}
var a3 = []int{1, 2, 3}

func main() {
	for i := range a1 {
		fmt.Printf("%v%c%v\n", a1[i], a2[i], a3[i])
	}
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-Over-Multiple-Tables.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A VALUE "abc".
           03  A-Vals PIC X OCCURS 3 TIMES.

       01  B VALUE "ABC".
           03  B-Vals PIC X OCCURS 3 TIMES.

       01  C VALUE "123".
           03  C-Vals PIC 9 OCCURS 3 TIMES.

       01  I PIC 9.

       PROCEDURE DIVISION.
           PERFORM VARYING I FROM 1 BY 1 UNTIL 3 < I
               DISPLAY A-Vals (I) B-Vals (I) C-Vals (I)
           END-PERFORM

           GOBACK
           .
Task: Loop over multiple arrays simultaneously






PHP: $a = array('a', 'b', 'c');
$b = array('A', 'B', 'C');
$c = array('1', '2', '3'); //These don't *have* to be strings, but it 
saves PHP from casting them later

if ((sizeOf($a) !== sizeOf($b)) || (sizeOf($b) !== sizeOf($c))){
  throw new Exception('All three arrays must be the same length');
}
foreach ($a as $key => $value){
  echo "{$a[$key]}{$b[$key]}{$c[$key]}\n";
}
Task: Loop over multiple arrays simultaneously






PHP: $a = array('a', 'b', 'c');
$b = array('A', 'B', 'C');
$c = array('1', '2', '3'); //These don't *have* to be strings, but it 
saves PHP from casting them later

if ((sizeOf($a) !== sizeOf($b)) || (sizeOf($b) !== sizeOf($c))){
  throw new Exception('All three arrays must be the same length');
}
foreach ($a as $key => $value){
  echo "{$a[$key]}{$b[$key]}{$c[$key]}\n";
}






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

procedure Array_Loop_Test is
   type Array_Index is range 1..3;
   A1 : array (Array_Index) of Character := "abc";
   A2 : array (Array_Index) of Character := "ABC";
   A3 : array (Array_Index) of Integer   := (1, 2, 3);
begin
   for Index in Array_Index'Range loop
      Put_Line (A1 (Index) & A2 (Index) & Integer'Image (A3 
(Index))(2));
   end loop;
end Array_Loop_Test;
Task: Loop over multiple arrays simultaneously






PHP: $a = array('a', 'b', 'c');
$b = array('A', 'B', 'C');
$c = array('1', '2', '3'); //These don't *have* to be strings, but it 
saves PHP from casting them later

if ((sizeOf($a) !== sizeOf($b)) || (sizeOf($b) !== sizeOf($c))){
  throw new Exception('All three arrays must be the same length');
}
foreach ($a as $key => $value){
  echo "{$a[$key]}{$b[$key]}{$c[$key]}\n";
}




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-Over-Multiple-Tables.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A VALUE "abc".
           03  A-Vals PIC X OCCURS 3 TIMES.

       01  B VALUE "ABC".
           03  B-Vals PIC X OCCURS 3 TIMES.

       01  C VALUE "123".
           03  C-Vals PIC 9 OCCURS 3 TIMES.

       01  I PIC 9.

       PROCEDURE DIVISION.
           PERFORM VARYING I FROM 1 BY 1 UNTIL 3 < I
               DISPLAY A-Vals (I) B-Vals (I) C-Vals (I)
           END-PERFORM

           GOBACK
           .
Task: Loop over multiple arrays simultaneously








VB: 

Sub Main()
	a = Array("a","b","c")
	b = Array("A","B","C")
	c = Array(1,2,3)
	For i = LBound(a) To UBound(a)
		buf = buf & vbCrLf & a(i) & b(i) & c(i)
	Next
	Debug.Print Mid(buf,3)
End Sub
Task: Sorting algorithms_Cocktail sort



C#: public static void cocktailSort(int[] A)
    {
        bool swapped;
        do
        {
            swapped = false;
            for (int i = 0; i <= A.Length - 2; i++)
            {
                if (A[i] > A[i + 1])
                {
                    
                    int temp = A[i];
                    A[i] = A[i + 1];
                    A[i + 1] = temp;
                    swapped = true;
                }
            }
            if (!swapped)
            {
                
                break;
            }
            swapped = false;
            for (int i = A.Length - 2; i >= 0; i--)
            {
                if (A[i] > A[i + 1])
                {
                    int temp = A[i];
                    A[i] = A[i + 1];
                    A[i + 1] = temp;
                    swapped = true;
                }
            }
            
        } while (swapped);
    }









Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Cocktail_Sort_Test is
   procedure Cocktail_Sort (Item : in out String) is
      procedure Swap(Left, Right : in out Character) is
         Temp : Character := Left;
      begin
         Left := Right;
         Right := Temp;
      end Swap;
      Swapped : Boolean := False;
   begin
      loop
         for I in 1..Item'Last - 1 loop
            if Item(I) > Item(I + 1) then
               Swap(Item(I), Item(I + 1));
               Swapped := True;
            end if;
         end loop;
         if not Swapped then
            for I in reverse 1..Item'Last - 1 loop
               if Item(I) > Item(I + 1) then
                  Swap(Item(I), Item(I + 1));
                  Swapped := True;
               end if;
            end loop;
         end if;
         exit when not Swapped;
         Swapped := False;
      end loop;
   end Cocktail_Sort;
   Data : String := "big fjords vex quick waltz nymph";
begin
   Put_Line(Data);
   Cocktail_Sort(Data);
   Put_Line(Data);
end Cocktail_Sort_Test;
Task: Sorting algorithms_Cocktail sort

C: #include <stdio.h>


void swap(int *x, int *y) {
	if(x == y)
		return;
	*x ^= *y;
	*y ^= *x;
	*x ^= *y;
}
void cocktailsort(int *a, size_t n) {
	while(1) {
		
		char flag;
		size_t start[2] = {1, n - 1},
			   end[2] = {n, 0},
			   inc[2] = {1, -1};
		for(int it = 0; it < 2; ++it) {
			flag = 1;
			for(int i = start[it]; i != end[it]; i += inc[it])
				if(a[i - 1] > a[i]) {
					swap(a + i - 1, a + i);
					flag = 0;
				}
			if(flag)
				return;
		}
	}
}

int main(void) {
	int a[] = { 5, -1, 101, -4, 0, 1, 8, 6, 2, 3 };
	size_t n = sizeof(a)/sizeof(a[0]);

	cocktailsort(a, n);
	for (size_t i = 0; i < n; ++i)
		printf("%d ", a[i]);
	return 0;
}











Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Cocktail_Sort_Test is
   procedure Cocktail_Sort (Item : in out String) is
      procedure Swap(Left, Right : in out Character) is
         Temp : Character := Left;
      begin
         Left := Right;
         Right := Temp;
      end Swap;
      Swapped : Boolean := False;
   begin
      loop
         for I in 1..Item'Last - 1 loop
            if Item(I) > Item(I + 1) then
               Swap(Item(I), Item(I + 1));
               Swapped := True;
            end if;
         end loop;
         if not Swapped then
            for I in reverse 1..Item'Last - 1 loop
               if Item(I) > Item(I + 1) then
                  Swap(Item(I), Item(I + 1));
                  Swapped := True;
               end if;
            end loop;
         end if;
         exit when not Swapped;
         Swapped := False;
      end loop;
   end Cocktail_Sort;
   Data : String := "big fjords vex quick waltz nymph";
begin
   Put_Line(Data);
   Cocktail_Sort(Data);
   Put_Line(Data);
end Cocktail_Sort_Test;
Task: Sorting algorithms_Cocktail sort


C++: #include <iostream>
#include <windows.h>

const int EL_COUNT = 77, LLEN = 11;

class cocktailSort
{
public:
    void sort( int* arr, int len )
    {
	bool notSorted = true;
	while( notSorted )
	{
	    notSorted = false;
	    for( int a = 0; a < len - 1; a++ )
	    {
		if( arr[a] > arr[a + 1] )
		{
		    sSwap( arr[a], arr[a + 1] );
		    notSorted = true;
		}
	    }
 
	    if( !notSorted ) break;
	    notSorted = false;
 
	    for( int a = len - 1; a > 0; a-- )
	    {
		if( arr[a - 1] > arr[a] )
		{
		    sSwap( arr[a], arr[a - 1] );
		    notSorted = true;
		}
	    }
	}
    }
 
private:
    void sSwap( int& a, int& b )
    {
	int t = a;
   	a = b; b = t;
    }
};

int main( int argc, char* argv[] )
{
    srand( GetTickCount() );
    cocktailSort cs;
    int arr[EL_COUNT];
 
    for( int x = 0; x < EL_COUNT; x++ )
        arr[x] = rand() % EL_COUNT + 1;
 
    std::cout << "Original: " << std::endl << "==========" << std::endl;
    for( int x = 0; x < EL_COUNT; x += LLEN )
    {
	for( int s = x; s < x + LLEN; s++ )
	    std::cout << arr[s] << ", ";
 
	std::cout << std::endl;
    }
 
    
    cs.sort( arr, EL_COUNT );
    
 
    std::cout << std::endl << std::endl << "Sorted: " << std::endl << "========" << std::endl;
    for( int x = 0; x < EL_COUNT; x += LLEN )
    {
	for( int s = x; s < x + LLEN; s++ )
	    std::cout << arr[s] << ", ";
 
	std::cout << std::endl;
    }
 
    std::cout << std::endl << std::endl << std::endl << std::endl;
    
    return 0;
}










Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Cocktail_Sort_Test is
   procedure Cocktail_Sort (Item : in out String) is
      procedure Swap(Left, Right : in out Character) is
         Temp : Character := Left;
      begin
         Left := Right;
         Right := Temp;
      end Swap;
      Swapped : Boolean := False;
   begin
      loop
         for I in 1..Item'Last - 1 loop
            if Item(I) > Item(I + 1) then
               Swap(Item(I), Item(I + 1));
               Swapped := True;
            end if;
         end loop;
         if not Swapped then
            for I in reverse 1..Item'Last - 1 loop
               if Item(I) > Item(I + 1) then
                  Swap(Item(I), Item(I + 1));
                  Swapped := True;
               end if;
            end loop;
         end if;
         exit when not Swapped;
         Swapped := False;
      end loop;
   end Cocktail_Sort;
   Data : String := "big fjords vex quick waltz nymph";
begin
   Put_Line(Data);
   Cocktail_Sort(Data);
   Put_Line(Data);
end Cocktail_Sort_Test;
Task: Sorting algorithms_Cocktail sort




Go: package main

import "fmt"

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

func cocktailSort(a []int) {
    last := len(a) - 1
    for {
        swapped := false
        for i := 0; i < last; i++ {
            if a[i] > a[i+1] {
                a[i], a[i+1] = a[i+1], a[i]
                swapped = true
            }
        }
        if !swapped {
            return
        }
        swapped = false
        for i := last - 1; i >= 0; i-- {
            if a[i] > a[i+1] {
                a[i], a[i+1] = a[i+1], a[i]
                swapped = true
            }
        }
        if !swapped {
            return
        }
    }
}








Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Cocktail_Sort_Test is
   procedure Cocktail_Sort (Item : in out String) is
      procedure Swap(Left, Right : in out Character) is
         Temp : Character := Left;
      begin
         Left := Right;
         Right := Temp;
      end Swap;
      Swapped : Boolean := False;
   begin
      loop
         for I in 1..Item'Last - 1 loop
            if Item(I) > Item(I + 1) then
               Swap(Item(I), Item(I + 1));
               Swapped := True;
            end if;
         end loop;
         if not Swapped then
            for I in reverse 1..Item'Last - 1 loop
               if Item(I) > Item(I + 1) then
                  Swap(Item(I), Item(I + 1));
                  Swapped := True;
               end if;
            end loop;
         end if;
         exit when not Swapped;
         Swapped := False;
      end loop;
   end Cocktail_Sort;
   Data : String := "big fjords vex quick waltz nymph";
begin
   Put_Line(Data);
   Cocktail_Sort(Data);
   Put_Line(Data);
end Cocktail_Sort_Test;
Task: Sorting algorithms_Cocktail sort





Java: public static void cocktailSort( int[] A ){
	boolean swapped;
	do {
		swapped = false;
		for (int i =0; i<=  A.length  - 2;i++) {
			if (A[ i ] > A[ i + 1 ]) {
				
				int temp = A[i];
				A[i] = A[i+1];
				A[i+1]=temp;
				swapped = true;
			}
		}
		if (!swapped) {
			
			break;
		}
		swapped = false;
		for (int i= A.length - 2;i>=0;i--) {
			if (A[ i ] > A[ i + 1 ]) {
				int temp = A[i];
				A[i] = A[i+1];
				A[i+1]=temp;
				swapped = true;
			}
		}
		
	} while (swapped);
}







Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Cocktail_Sort_Test is
   procedure Cocktail_Sort (Item : in out String) is
      procedure Swap(Left, Right : in out Character) is
         Temp : Character := Left;
      begin
         Left := Right;
         Right := Temp;
      end Swap;
      Swapped : Boolean := False;
   begin
      loop
         for I in 1..Item'Last - 1 loop
            if Item(I) > Item(I + 1) then
               Swap(Item(I), Item(I + 1));
               Swapped := True;
            end if;
         end loop;
         if not Swapped then
            for I in reverse 1..Item'Last - 1 loop
               if Item(I) > Item(I + 1) then
                  Swap(Item(I), Item(I + 1));
                  Swapped := True;
               end if;
            end loop;
         end if;
         exit when not Swapped;
         Swapped := False;
      end loop;
   end Cocktail_Sort;
   Data : String := "big fjords vex quick waltz nymph";
begin
   Put_Line(Data);
   Cocktail_Sort(Data);
   Put_Line(Data);
end Cocktail_Sort_Test;
Task: Sorting algorithms_Cocktail sort







Python: def cocktailSort(A):
    up = range(len(A)-1)
    while True:
        for indices in (up, reversed(up)):
            swapped = False
            for i in indices:
                if A[i] > A[i+1]:  
                    A[i], A[i+1] =  A[i+1], A[i]
                    swapped = True
            if not swapped:
                return





Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Cocktail_Sort_Test is
   procedure Cocktail_Sort (Item : in out String) is
      procedure Swap(Left, Right : in out Character) is
         Temp : Character := Left;
      begin
         Left := Right;
         Right := Temp;
      end Swap;
      Swapped : Boolean := False;
   begin
      loop
         for I in 1..Item'Last - 1 loop
            if Item(I) > Item(I + 1) then
               Swap(Item(I), Item(I + 1));
               Swapped := True;
            end if;
         end loop;
         if not Swapped then
            for I in reverse 1..Item'Last - 1 loop
               if Item(I) > Item(I + 1) then
                  Swap(Item(I), Item(I + 1));
                  Swapped := True;
               end if;
            end loop;
         end if;
         exit when not Swapped;
         Swapped := False;
      end loop;
   end Cocktail_Sort;
   Data : String := "big fjords vex quick waltz nymph";
begin
   Put_Line(Data);
   Cocktail_Sort(Data);
   Put_Line(Data);
end Cocktail_Sort_Test;
Task: Sorting algorithms_Cocktail sort








VB: Function cocktail_sort(ByVal s As Variant) As Variant
    Dim swapped As Boolean
    Dim f As Integer, t As Integer, d As Integer, tmp As Integer
    swapped = True
    f = 1
    t = UBound(s) - 1
    d = 1
    Do While swapped
        swapped = 0
        For i = f To t Step d
            If Val(s(i)) > Val(s(i + 1)) Then
                tmp = s(i)
                s(i) = s(i + 1)
                s(i + 1) = tmp
                swapped = True
            End If
        Next i
        
        
        
        tmp = f
        f = t + (d = 1)
        t = tmp + (d = -1)
        d = -d
    Loop
    cocktail_sort = s
End Function
 
Public Sub main()
    Dim s(9) As Variant
    For i = 0 To 9
        s(i) = CStr(Int(1000 * Rnd))
    Next i
    Debug.Print Join(s, ", ")
    Debug.Print Join(cocktail_sort(s), ", ")
End Sub




Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Cocktail_Sort_Test is
   procedure Cocktail_Sort (Item : in out String) is
      procedure Swap(Left, Right : in out Character) is
         Temp : Character := Left;
      begin
         Left := Right;
         Right := Temp;
      end Swap;
      Swapped : Boolean := False;
   begin
      loop
         for I in 1..Item'Last - 1 loop
            if Item(I) > Item(I + 1) then
               Swap(Item(I), Item(I + 1));
               Swapped := True;
            end if;
         end loop;
         if not Swapped then
            for I in reverse 1..Item'Last - 1 loop
               if Item(I) > Item(I + 1) then
                  Swap(Item(I), Item(I + 1));
                  Swapped := True;
               end if;
            end loop;
         end if;
         exit when not Swapped;
         Swapped := False;
      end loop;
   end Cocktail_Sort;
   Data : String := "big fjords vex quick waltz nymph";
begin
   Put_Line(Data);
   Cocktail_Sort(Data);
   Put_Line(Data);
end Cocktail_Sort_Test;
Task: Sorting algorithms_Cocktail sort

C: #include <stdio.h>


void swap(int *x, int *y) {
	if(x == y)
		return;
	*x ^= *y;
	*y ^= *x;
	*x ^= *y;
}
void cocktailsort(int *a, size_t n) {
	while(1) {
		
		char flag;
		size_t start[2] = {1, n - 1},
			   end[2] = {n, 0},
			   inc[2] = {1, -1};
		for(int it = 0; it < 2; ++it) {
			flag = 1;
			for(int i = start[it]; i != end[it]; i += inc[it])
				if(a[i - 1] > a[i]) {
					swap(a + i - 1, a + i);
					flag = 0;
				}
			if(flag)
				return;
		}
	}
}

int main(void) {
	int a[] = { 5, -1, 101, -4, 0, 1, 8, 6, 2, 3 };
	size_t n = sizeof(a)/sizeof(a[0]);

	cocktailsort(a, n);
	for (size_t i = 0; i < n; ++i)
		printf("%d ", a[i]);
	return 0;
}
Task: Sorting algorithms_Cocktail sort



C#: public static void cocktailSort(int[] A)
    {
        bool swapped;
        do
        {
            swapped = false;
            for (int i = 0; i <= A.Length - 2; i++)
            {
                if (A[i] > A[i + 1])
                {
                    
                    int temp = A[i];
                    A[i] = A[i + 1];
                    A[i + 1] = temp;
                    swapped = true;
                }
            }
            if (!swapped)
            {
                
                break;
            }
            swapped = false;
            for (int i = A.Length - 2; i >= 0; i--)
            {
                if (A[i] > A[i + 1])
                {
                    int temp = A[i];
                    A[i] = A[i + 1];
                    A[i + 1] = temp;
                    swapped = true;
                }
            }
            
        } while (swapped);
    }