Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Write the same code in C++ as shown below in Delphi.
program TestShakerSort; type TItem = Integer; TArray = array of TItem; TArray = array[0..15] of TItem; procedure ShakerSort(var A: TArray); var Item: TItem; K, L, R, J: Integer; begin L:= Low(A) + 1; R:= High(A); K:= High(A); repeat for J:= R downto L do begin if ...
#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], ...
Produce a functionally identical Java code for the snippet given in Delphi.
program TestShakerSort; type TItem = Integer; TArray = array of TItem; TArray = array[0..15] of TItem; procedure ShakerSort(var A: TArray); var Item: TItem; K, L, R, J: Integer; begin L:= Low(A) + 1; R:= High(A); K:= High(A); repeat for J:= R downto L do begin if ...
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= ...
Convert this Delphi block to Python, preserving its control flow and logic.
program TestShakerSort; type TItem = Integer; TArray = array of TItem; TArray = array[0..15] of TItem; procedure ShakerSort(var A: TArray); var Item: TItem; K, L, R, J: Integer; begin L:= Low(A) + 1; R:= High(A); K:= High(A); repeat for J:= R downto L do begin if ...
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: ...
Generate an equivalent VB version of this Delphi code.
program TestShakerSort; type TItem = Integer; TArray = array of TItem; TArray = array[0..15] of TItem; procedure ShakerSort(var A: TArray); var Item: TItem; K, L, R, J: Integer; begin L:= Low(A) + 1; R:= High(A); K:= High(A); repeat for J:= R downto L do begin if ...
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)) T...
Change the programming language of this snippet from Delphi to Go without modifying what it does.
program TestShakerSort; type TItem = Integer; TArray = array of TItem; TArray = array[0..15] of TItem; procedure ShakerSort(var A: TArray); var Item: TItem; K, L, R, J: Integer; begin L:= Low(A) + 1; R:= High(A); K:= High(A); repeat for J:= R downto L do begin if ...
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] > ...
Port the provided Elixir code into C while preserving the original functionality.
defmodule Sort do def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], []) defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist) defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist]) defp cocktail_sort(list, minlist, maxlist) do {max...
#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[i...
Convert this Elixir snippet to C# and keep its semantics consistent.
defmodule Sort do def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], []) defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist) defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist]) defp cocktail_sort(list, minlist, maxlist) do {max...
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[...
Produce a language-to-language conversion: from Elixir to C++, same semantics.
defmodule Sort do def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], []) defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist) defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist]) defp cocktail_sort(list, minlist, maxlist) do {max...
#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], ...
Convert the following code from Elixir to Java, ensuring the logic remains intact.
defmodule Sort do def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], []) defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist) defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist]) defp cocktail_sort(list, minlist, maxlist) do {max...
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= ...
Can you help me rewrite this code in VB instead of Elixir, keeping it the same logically?
defmodule Sort do def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], []) defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist) defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist]) defp cocktail_sort(list, minlist, maxlist) do {max...
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)) T...
Rewrite the snippet below in Go so it works the same as the original Elixir code.
defmodule Sort do def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], []) defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist) defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist]) defp cocktail_sort(list, minlist, maxlist) do {max...
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] > ...
Keep all operations the same but rewrite the snippet in C.
USING: kernel locals math math.ranges sequences ; :: cocktail-sort f :> swapped [ swapped ] [ f swapped seq length 2 - [| i | i i 1 + [ seq nth ] bi@ > [ i i 1 + seq exchange t swa...
#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[i...
Change the programming language of this snippet from Factor to C# without modifying what it does.
USING: kernel locals math math.ranges sequences ; :: cocktail-sort f :> swapped [ swapped ] [ f swapped seq length 2 - [| i | i i 1 + [ seq nth ] bi@ > [ i i 1 + seq exchange t swa...
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[...
Rewrite the snippet below in C++ so it works the same as the original Factor code.
USING: kernel locals math math.ranges sequences ; :: cocktail-sort f :> swapped [ swapped ] [ f swapped seq length 2 - [| i | i i 1 + [ seq nth ] bi@ > [ i i 1 + seq exchange t swa...
#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], ...
Produce a language-to-language conversion: from Factor to Java, same semantics.
USING: kernel locals math math.ranges sequences ; :: cocktail-sort f :> swapped [ swapped ] [ f swapped seq length 2 - [| i | i i 1 + [ seq nth ] bi@ > [ i i 1 + seq exchange t swa...
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= ...
Write the same algorithm in Python as shown in this Factor implementation.
USING: kernel locals math math.ranges sequences ; :: cocktail-sort f :> swapped [ swapped ] [ f swapped seq length 2 - [| i | i i 1 + [ seq nth ] bi@ > [ i i 1 + seq exchange t swa...
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: ...
Change the following Factor code into VB without altering its purpose.
USING: kernel locals math math.ranges sequences ; :: cocktail-sort f :> swapped [ swapped ] [ f swapped seq length 2 - [| i | i i 1 + [ seq nth ] bi@ > [ i i 1 + seq exchange t swa...
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)) T...
Rewrite this program in Go while keeping its functionality equivalent to the Factor version.
USING: kernel locals math math.ranges sequences ; :: cocktail-sort f :> swapped [ swapped ] [ f swapped seq length 2 - [| i | i i 1 + [ seq nth ] bi@ > [ i i 1 + seq exchange t swa...
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] > ...
Generate an equivalent C version of this Forth code.
defer precedes : sort 1- cells bounds 2>r false begin 0= dup while 2r@ ?do i cell+ @ i @ over over precedes if i cell+ ! i ! dup xor else drop drop then 1 cells +loop 0= dup ...
#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[i...
Write a version of this Forth function in C# with identical behavior.
defer precedes : sort 1- cells bounds 2>r false begin 0= dup while 2r@ ?do i cell+ @ i @ over over precedes if i cell+ ! i ! dup xor else drop drop then 1 cells +loop 0= dup ...
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[...
Produce a functionally identical C++ code for the snippet given in Forth.
defer precedes : sort 1- cells bounds 2>r false begin 0= dup while 2r@ ?do i cell+ @ i @ over over precedes if i cell+ ! i ! dup xor else drop drop then 1 cells +loop 0= dup ...
#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], ...
Generate a Java translation of this Forth snippet without changing its computational steps.
defer precedes : sort 1- cells bounds 2>r false begin 0= dup while 2r@ ?do i cell+ @ i @ over over precedes if i cell+ ! i ! dup xor else drop drop then 1 cells +loop 0= dup ...
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= ...
Write a version of this Forth function in Python with identical behavior.
defer precedes : sort 1- cells bounds 2>r false begin 0= dup while 2r@ ?do i cell+ @ i @ over over precedes if i cell+ ! i ! dup xor else drop drop then 1 cells +loop 0= dup ...
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: ...
Convert this Forth block to VB, preserving its control flow and logic.
defer precedes : sort 1- cells bounds 2>r false begin 0= dup while 2r@ ?do i cell+ @ i @ over over precedes if i cell+ ! i ! dup xor else drop drop then 1 cells +loop 0= dup ...
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)) T...
Translate this program into Go but keep the logic exactly as in Forth.
defer precedes : sort 1- cells bounds 2>r false begin 0= dup while 2r@ ?do i cell+ @ i @ over over precedes if i cell+ ! i ! dup xor else drop drop then 1 cells +loop 0= dup ...
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] > ...
Maintain the same structure and functionality when rewriting this code in C#.
PROGRAM COCKTAIL IMPLICIT NONE INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /) WRITE(*,"(A,10I5)") "Unsorted array:", intArray CALL Cocktail_sort(intArray) WRITE(*,"(A,10I5)") "Sorted array  :", intArray CONTAINS SUBROUTINE Cocktail_sort(a) INTEGER, INTENT(IN OUT) :: a(:) INTEGE...
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[...
Produce a language-to-language conversion: from Fortran to C++, same semantics.
PROGRAM COCKTAIL IMPLICIT NONE INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /) WRITE(*,"(A,10I5)") "Unsorted array:", intArray CALL Cocktail_sort(intArray) WRITE(*,"(A,10I5)") "Sorted array  :", intArray CONTAINS SUBROUTINE Cocktail_sort(a) INTEGER, INTENT(IN OUT) :: a(:) INTEGE...
#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], ...
Transform the following Fortran implementation into C, maintaining the same output and logic.
PROGRAM COCKTAIL IMPLICIT NONE INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /) WRITE(*,"(A,10I5)") "Unsorted array:", intArray CALL Cocktail_sort(intArray) WRITE(*,"(A,10I5)") "Sorted array  :", intArray CONTAINS SUBROUTINE Cocktail_sort(a) INTEGER, INTENT(IN OUT) :: a(:) INTEGE...
#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[i...
Port the following code from Fortran to Go with equivalent syntax and logic.
PROGRAM COCKTAIL IMPLICIT NONE INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /) WRITE(*,"(A,10I5)") "Unsorted array:", intArray CALL Cocktail_sort(intArray) WRITE(*,"(A,10I5)") "Sorted array  :", intArray CONTAINS SUBROUTINE Cocktail_sort(a) INTEGER, INTENT(IN OUT) :: a(:) INTEGE...
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] > ...
Change the following Fortran code into Java without altering its purpose.
PROGRAM COCKTAIL IMPLICIT NONE INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /) WRITE(*,"(A,10I5)") "Unsorted array:", intArray CALL Cocktail_sort(intArray) WRITE(*,"(A,10I5)") "Sorted array  :", intArray CONTAINS SUBROUTINE Cocktail_sort(a) INTEGER, INTENT(IN OUT) :: a(:) INTEGE...
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= ...
Write a version of this Fortran function in Python with identical behavior.
PROGRAM COCKTAIL IMPLICIT NONE INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /) WRITE(*,"(A,10I5)") "Unsorted array:", intArray CALL Cocktail_sort(intArray) WRITE(*,"(A,10I5)") "Sorted array  :", intArray CONTAINS SUBROUTINE Cocktail_sort(a) INTEGER, INTENT(IN OUT) :: a(:) INTEGE...
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: ...
Port the following code from Fortran to PHP with equivalent syntax and logic.
PROGRAM COCKTAIL IMPLICIT NONE INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /) WRITE(*,"(A,10I5)") "Unsorted array:", intArray CALL Cocktail_sort(intArray) WRITE(*,"(A,10I5)") "Sorted array  :", intArray CONTAINS SUBROUTINE Cocktail_sort(a) INTEGER, INTENT(IN OUT) :: a(:) INTEGE...
function cocktailSort($arr){ if (is_string($arr)) $arr = str_split(preg_replace('/\s+/','',$arr)); do{ $swapped = false; for($i=0;$i<count($arr);$i++){ if(isset($arr[$i+1])){ if($arr[$i] > $arr[$i+1]){ list($arr[$i], $arr[$i+1]) = array($arr[$i+1], $arr[$i]); $swapped = true; } } } ...
Port the provided Groovy code into C while preserving the original functionality.
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } } def cocktailSort = { list -> if (list == null || list.size() < 2) return list def n = list.size() def swap = checkSwap.curry(list) while (true)...
#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[i...
Port the provided Groovy code into C# while preserving the original functionality.
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } } def cocktailSort = { list -> if (list == null || list.size() < 2) return list def n = list.size() def swap = checkSwap.curry(list) while (true)...
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[...
Can you help me rewrite this code in C++ instead of Groovy, keeping it the same logically?
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } } def cocktailSort = { list -> if (list == null || list.size() < 2) return list def n = list.size() def swap = checkSwap.curry(list) while (true)...
#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], ...
Transform the following Groovy implementation into Java, maintaining the same output and logic.
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } } def cocktailSort = { list -> if (list == null || list.size() < 2) return list def n = list.size() def swap = checkSwap.curry(list) while (true)...
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= ...
Maintain the same structure and functionality when rewriting this code in Python.
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } } def cocktailSort = { list -> if (list == null || list.size() < 2) return list def n = list.size() def swap = checkSwap.curry(list) while (true)...
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: ...
Translate this program into VB but keep the logic exactly as in Groovy.
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } } def cocktailSort = { list -> if (list == null || list.size() < 2) return list def n = list.size() def swap = checkSwap.curry(list) while (true)...
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)) T...
Write a version of this Groovy function in Go with identical behavior.
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } } def cocktailSort = { list -> if (list == null || list.size() < 2) return list def n = list.size() def swap = checkSwap.curry(list) while (true)...
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] > ...
Change the following Haskell code into C without altering its purpose.
cocktailSort :: Ord a => [a] -> [a] cocktailSort l | not swapped1 = l | not swapped2 = reverse $ l1 | otherwise = cocktailSort l2 where (swapped1, l1) = swappingPass (>) (False, []) l (swapped2, l2) = swappingPass (<) (False, []) l1 swappingPass :: Ord a => (a -> a -> Bool) -> (Bool, [a]) ->...
#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[i...
Write the same code in C# as shown below in Haskell.
cocktailSort :: Ord a => [a] -> [a] cocktailSort l | not swapped1 = l | not swapped2 = reverse $ l1 | otherwise = cocktailSort l2 where (swapped1, l1) = swappingPass (>) (False, []) l (swapped2, l2) = swappingPass (<) (False, []) l1 swappingPass :: Ord a => (a -> a -> Bool) -> (Bool, [a]) ->...
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[...
Generate an equivalent C++ version of this Haskell code.
cocktailSort :: Ord a => [a] -> [a] cocktailSort l | not swapped1 = l | not swapped2 = reverse $ l1 | otherwise = cocktailSort l2 where (swapped1, l1) = swappingPass (>) (False, []) l (swapped2, l2) = swappingPass (<) (False, []) l1 swappingPass :: Ord a => (a -> a -> Bool) -> (Bool, [a]) ->...
#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], ...
Generate a Java translation of this Haskell snippet without changing its computational steps.
cocktailSort :: Ord a => [a] -> [a] cocktailSort l | not swapped1 = l | not swapped2 = reverse $ l1 | otherwise = cocktailSort l2 where (swapped1, l1) = swappingPass (>) (False, []) l (swapped2, l2) = swappingPass (<) (False, []) l1 swappingPass :: Ord a => (a -> a -> Bool) -> (Bool, [a]) ->...
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= ...
Convert the following code from Haskell to Python, ensuring the logic remains intact.
cocktailSort :: Ord a => [a] -> [a] cocktailSort l | not swapped1 = l | not swapped2 = reverse $ l1 | otherwise = cocktailSort l2 where (swapped1, l1) = swappingPass (>) (False, []) l (swapped2, l2) = swappingPass (<) (False, []) l1 swappingPass :: Ord a => (a -> a -> Bool) -> (Bool, [a]) ->...
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: ...
Port the provided Haskell code into VB while preserving the original functionality.
cocktailSort :: Ord a => [a] -> [a] cocktailSort l | not swapped1 = l | not swapped2 = reverse $ l1 | otherwise = cocktailSort l2 where (swapped1, l1) = swappingPass (>) (False, []) l (swapped2, l2) = swappingPass (<) (False, []) l1 swappingPass :: Ord a => (a -> a -> Bool) -> (Bool, [a]) ->...
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)) T...
Ensure the translated Go code behaves exactly like the original Haskell snippet.
cocktailSort :: Ord a => [a] -> [a] cocktailSort l | not swapped1 = l | not swapped2 = reverse $ l1 | otherwise = cocktailSort l2 where (swapped1, l1) = swappingPass (>) (False, []) l (swapped2, l2) = swappingPass (<) (False, []) l1 swappingPass :: Ord a => (a -> a -> Bool) -> (Bool, [a]) ->...
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] > ...
Port the provided Icon code into C while preserving the original functionality.
procedure main() demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end procedure cocktailsort(X,op) local i,swapped op := sortop(op,X) swapped := 1 repeat every (if /swapped then break break else swapped := &null &...
#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[i...
Write the same algorithm in C# as shown in this Icon implementation.
procedure main() demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end procedure cocktailsort(X,op) local i,swapped op := sortop(op,X) swapped := 1 repeat every (if /swapped then break break else swapped := &null &...
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[...
Can you help me rewrite this code in C++ instead of Icon, keeping it the same logically?
procedure main() demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end procedure cocktailsort(X,op) local i,swapped op := sortop(op,X) swapped := 1 repeat every (if /swapped then break break else swapped := &null &...
#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], ...
Generate a Java translation of this Icon snippet without changing its computational steps.
procedure main() demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end procedure cocktailsort(X,op) local i,swapped op := sortop(op,X) swapped := 1 repeat every (if /swapped then break break else swapped := &null &...
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= ...
Produce a functionally identical Python code for the snippet given in Icon.
procedure main() demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end procedure cocktailsort(X,op) local i,swapped op := sortop(op,X) swapped := 1 repeat every (if /swapped then break break else swapped := &null &...
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: ...
Maintain the same structure and functionality when rewriting this code in VB.
procedure main() demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end procedure cocktailsort(X,op) local i,swapped op := sortop(op,X) swapped := 1 repeat every (if /swapped then break break else swapped := &null &...
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)) T...
Generate a Go translation of this Icon snippet without changing its computational steps.
procedure main() demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end procedure cocktailsort(X,op) local i,swapped op := sortop(op,X) swapped := 1 repeat every (if /swapped then break break else swapped := &null &...
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] > ...
Change the programming language of this snippet from J to C without modifying what it does.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
#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[i...
Produce a language-to-language conversion: from J to C#, same semantics.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
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[...
Generate an equivalent C++ version of this J code.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
#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], ...
Change the following J code into Java without altering its purpose.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
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= ...
Convert this J snippet to Python and keep its semantics consistent.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
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: ...
Generate a VB translation of this J snippet without changing its computational steps.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
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)) T...
Convert this J snippet to Go and keep its semantics consistent.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
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] > ...
Produce a functionally identical C code for the snippet given in Julia.
function cocktailsort(a::Vector) b = copy(a) isordered = false lo, hi = 1, length(b) while !isordered && hi > lo isordered = true for i in lo+1:hi if b[i] < b[i-1] b[i-1], b[i] = b[i], b[i-1] isordered = false end end ...
#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[i...
Translate the given Julia code snippet into C# without altering its behavior.
function cocktailsort(a::Vector) b = copy(a) isordered = false lo, hi = 1, length(b) while !isordered && hi > lo isordered = true for i in lo+1:hi if b[i] < b[i-1] b[i-1], b[i] = b[i], b[i-1] isordered = false end end ...
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[...
Translate the given Julia code snippet into C++ without altering its behavior.
function cocktailsort(a::Vector) b = copy(a) isordered = false lo, hi = 1, length(b) while !isordered && hi > lo isordered = true for i in lo+1:hi if b[i] < b[i-1] b[i-1], b[i] = b[i], b[i-1] isordered = false end end ...
#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], ...
Change the programming language of this snippet from Julia to Java without modifying what it does.
function cocktailsort(a::Vector) b = copy(a) isordered = false lo, hi = 1, length(b) while !isordered && hi > lo isordered = true for i in lo+1:hi if b[i] < b[i-1] b[i-1], b[i] = b[i], b[i-1] isordered = false end end ...
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= ...
Change the programming language of this snippet from Julia to Python without modifying what it does.
function cocktailsort(a::Vector) b = copy(a) isordered = false lo, hi = 1, length(b) while !isordered && hi > lo isordered = true for i in lo+1:hi if b[i] < b[i-1] b[i-1], b[i] = b[i], b[i-1] isordered = false end end ...
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: ...
Rewrite this program in VB while keeping its functionality equivalent to the Julia version.
function cocktailsort(a::Vector) b = copy(a) isordered = false lo, hi = 1, length(b) while !isordered && hi > lo isordered = true for i in lo+1:hi if b[i] < b[i-1] b[i-1], b[i] = b[i], b[i-1] isordered = false end end ...
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)) T...
Generate a Go translation of this Julia snippet without changing its computational steps.
function cocktailsort(a::Vector) b = copy(a) isordered = false lo, hi = 1, length(b) while !isordered && hi > lo isordered = true for i in lo+1:hi if b[i] < b[i-1] b[i-1], b[i] = b[i], b[i-1] isordered = false end end ...
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] > ...
Write a version of this Lua function in C with identical behavior.
function cocktailSort( A ) local swapped repeat swapped = false for i = 1, #A - 1 do if A[ i ] > A[ i+1 ] then A[ i ], A[ i+1 ] = A[ i+1 ] ,A[i] swapped=true end end if swapped == false then break end for i = #A - 1,1,-1 do if A[ i ] > A[ i+1 ] then ...
#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[i...
Change the following Lua code into C# without altering its purpose.
function cocktailSort( A ) local swapped repeat swapped = false for i = 1, #A - 1 do if A[ i ] > A[ i+1 ] then A[ i ], A[ i+1 ] = A[ i+1 ] ,A[i] swapped=true end end if swapped == false then break end for i = #A - 1,1,-1 do if A[ i ] > A[ i+1 ] then ...
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[...
Rewrite this program in C++ while keeping its functionality equivalent to the Lua version.
function cocktailSort( A ) local swapped repeat swapped = false for i = 1, #A - 1 do if A[ i ] > A[ i+1 ] then A[ i ], A[ i+1 ] = A[ i+1 ] ,A[i] swapped=true end end if swapped == false then break end for i = #A - 1,1,-1 do if A[ i ] > A[ i+1 ] then ...
#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], ...
Maintain the same structure and functionality when rewriting this code in Java.
function cocktailSort( A ) local swapped repeat swapped = false for i = 1, #A - 1 do if A[ i ] > A[ i+1 ] then A[ i ], A[ i+1 ] = A[ i+1 ] ,A[i] swapped=true end end if swapped == false then break end for i = #A - 1,1,-1 do if A[ i ] > A[ i+1 ] then ...
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= ...
Convert this Lua block to Python, preserving its control flow and logic.
function cocktailSort( A ) local swapped repeat swapped = false for i = 1, #A - 1 do if A[ i ] > A[ i+1 ] then A[ i ], A[ i+1 ] = A[ i+1 ] ,A[i] swapped=true end end if swapped == false then break end for i = #A - 1,1,-1 do if A[ i ] > A[ i+1 ] then ...
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: ...
Maintain the same structure and functionality when rewriting this code in VB.
function cocktailSort( A ) local swapped repeat swapped = false for i = 1, #A - 1 do if A[ i ] > A[ i+1 ] then A[ i ], A[ i+1 ] = A[ i+1 ] ,A[i] swapped=true end end if swapped == false then break end for i = #A - 1,1,-1 do if A[ i ] > A[ i+1 ] then ...
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)) T...
Please provide an equivalent version of this Lua code in Go.
function cocktailSort( A ) local swapped repeat swapped = false for i = 1, #A - 1 do if A[ i ] > A[ i+1 ] then A[ i ], A[ i+1 ] = A[ i+1 ] ,A[i] swapped=true end end if swapped == false then break end for i = #A - 1,1,-1 do if A[ i ] > A[ i+1 ] then ...
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] > ...
Port the following code from Mathematica to C with equivalent syntax and logic.
cocktailSort[A_List] := Module[ { swapped = True }, While[ swapped == True, swapped=False; For[ i = 1, i< Length[A]-1,i++, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped=True;] ]; If[swapped == False, Break[]]; swapped=False; For [ i= Length[A]-1, i > 0, i--, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] =...
#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[i...
Write a version of this Mathematica function in C# with identical behavior.
cocktailSort[A_List] := Module[ { swapped = True }, While[ swapped == True, swapped=False; For[ i = 1, i< Length[A]-1,i++, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped=True;] ]; If[swapped == False, Break[]]; swapped=False; For [ i= Length[A]-1, i > 0, i--, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] =...
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[...
Generate an equivalent C++ version of this Mathematica code.
cocktailSort[A_List] := Module[ { swapped = True }, While[ swapped == True, swapped=False; For[ i = 1, i< Length[A]-1,i++, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped=True;] ]; If[swapped == False, Break[]]; swapped=False; For [ i= Length[A]-1, i > 0, i--, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] =...
#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], ...
Generate an equivalent Java version of this Mathematica code.
cocktailSort[A_List] := Module[ { swapped = True }, While[ swapped == True, swapped=False; For[ i = 1, i< Length[A]-1,i++, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped=True;] ]; If[swapped == False, Break[]]; swapped=False; For [ i= Length[A]-1, i > 0, i--, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] =...
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= ...
Change the following Mathematica code into Python without altering its purpose.
cocktailSort[A_List] := Module[ { swapped = True }, While[ swapped == True, swapped=False; For[ i = 1, i< Length[A]-1,i++, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped=True;] ]; If[swapped == False, Break[]]; swapped=False; For [ i= Length[A]-1, i > 0, i--, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] =...
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: ...
Change the following Mathematica code into VB without altering its purpose.
cocktailSort[A_List] := Module[ { swapped = True }, While[ swapped == True, swapped=False; For[ i = 1, i< Length[A]-1,i++, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped=True;] ]; If[swapped == False, Break[]]; swapped=False; For [ i= Length[A]-1, i > 0, i--, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] =...
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)) T...
Transform the following Mathematica implementation into Go, maintaining the same output and logic.
cocktailSort[A_List] := Module[ { swapped = True }, While[ swapped == True, swapped=False; For[ i = 1, i< Length[A]-1,i++, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped=True;] ]; If[swapped == False, Break[]]; swapped=False; For [ i= Length[A]-1, i > 0, i--, If[ A[[i]] > A[[i+1]], A[[i;;i+1]] =...
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] > ...
Transform the following MATLAB implementation into C, maintaining the same output and logic.
function list = cocktailSort(list) swapped = true; while swapped swapped = false; for i = (1:numel(list)-1) if( list(i) > list(i+1) ) list([i i+1]) = list([i+1 i]); swapped = true; end end if ~s...
#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[i...
Can you help me rewrite this code in C# instead of MATLAB, keeping it the same logically?
function list = cocktailSort(list) swapped = true; while swapped swapped = false; for i = (1:numel(list)-1) if( list(i) > list(i+1) ) list([i i+1]) = list([i+1 i]); swapped = true; end end if ~s...
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[...
Port the provided MATLAB code into C++ while preserving the original functionality.
function list = cocktailSort(list) swapped = true; while swapped swapped = false; for i = (1:numel(list)-1) if( list(i) > list(i+1) ) list([i i+1]) = list([i+1 i]); swapped = true; end end if ~s...
#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], ...
Produce a functionally identical Java code for the snippet given in MATLAB.
function list = cocktailSort(list) swapped = true; while swapped swapped = false; for i = (1:numel(list)-1) if( list(i) > list(i+1) ) list([i i+1]) = list([i+1 i]); swapped = true; end end if ~s...
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= ...
Rewrite the snippet below in Python so it works the same as the original MATLAB code.
function list = cocktailSort(list) swapped = true; while swapped swapped = false; for i = (1:numel(list)-1) if( list(i) > list(i+1) ) list([i i+1]) = list([i+1 i]); swapped = true; end end if ~s...
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: ...
Transform the following MATLAB implementation into VB, maintaining the same output and logic.
function list = cocktailSort(list) swapped = true; while swapped swapped = false; for i = (1:numel(list)-1) if( list(i) > list(i+1) ) list([i i+1]) = list([i+1 i]); swapped = true; end end if ~s...
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)) T...
Translate the given MATLAB code snippet into Go without altering its behavior.
function list = cocktailSort(list) swapped = true; while swapped swapped = false; for i = (1:numel(list)-1) if( list(i) > list(i+1) ) list([i i+1]) = list([i+1 i]); swapped = true; end end if ~s...
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] > ...
Produce a language-to-language conversion: from Nim to C, same semantics.
template trySwap(): untyped = if a[i] < a[i-1]: swap a[i], a[i-1] t = false proc cocktailSort[T](a: var openarray[T]) = var t = false var l = a.len while not t: t = true for i in 1 ..< l: trySwap if t: break for i in countdown(l-1, 1): trySwap var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782...
#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[i...
Generate an equivalent C# version of this Nim code.
template trySwap(): untyped = if a[i] < a[i-1]: swap a[i], a[i-1] t = false proc cocktailSort[T](a: var openarray[T]) = var t = false var l = a.len while not t: t = true for i in 1 ..< l: trySwap if t: break for i in countdown(l-1, 1): trySwap var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782...
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[...
Port the provided Nim code into C++ while preserving the original functionality.
template trySwap(): untyped = if a[i] < a[i-1]: swap a[i], a[i-1] t = false proc cocktailSort[T](a: var openarray[T]) = var t = false var l = a.len while not t: t = true for i in 1 ..< l: trySwap if t: break for i in countdown(l-1, 1): trySwap var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782...
#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], ...
Write the same code in Java as shown below in Nim.
template trySwap(): untyped = if a[i] < a[i-1]: swap a[i], a[i-1] t = false proc cocktailSort[T](a: var openarray[T]) = var t = false var l = a.len while not t: t = true for i in 1 ..< l: trySwap if t: break for i in countdown(l-1, 1): trySwap var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782...
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= ...
Write the same algorithm in Python as shown in this Nim implementation.
template trySwap(): untyped = if a[i] < a[i-1]: swap a[i], a[i-1] t = false proc cocktailSort[T](a: var openarray[T]) = var t = false var l = a.len while not t: t = true for i in 1 ..< l: trySwap if t: break for i in countdown(l-1, 1): trySwap var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782...
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: ...
Can you help me rewrite this code in VB instead of Nim, keeping it the same logically?
template trySwap(): untyped = if a[i] < a[i-1]: swap a[i], a[i-1] t = false proc cocktailSort[T](a: var openarray[T]) = var t = false var l = a.len while not t: t = true for i in 1 ..< l: trySwap if t: break for i in countdown(l-1, 1): trySwap var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782...
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)) T...
Convert the following code from Nim to Go, ensuring the logic remains intact.
template trySwap(): untyped = if a[i] < a[i-1]: swap a[i], a[i-1] t = false proc cocktailSort[T](a: var openarray[T]) = var t = false var l = a.len while not t: t = true for i in 1 ..< l: trySwap if t: break for i in countdown(l-1, 1): trySwap var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782...
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] > ...
Generate an equivalent C version of this OCaml code.
let swap a i j = let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp; ;; let cocktail_sort a = let begin_ = ref(-1) and end_ = ref(Array.length a - 2) in let swapped = ref true in try while !swapped do swapped := false; incr begin_; for i = !begin_ to !end_ do if a.(i) > a.(i+1) then begin ...
#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[i...
Preserve the algorithm and functionality while converting the code from OCaml to C#.
let swap a i j = let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp; ;; let cocktail_sort a = let begin_ = ref(-1) and end_ = ref(Array.length a - 2) in let swapped = ref true in try while !swapped do swapped := false; incr begin_; for i = !begin_ to !end_ do if a.(i) > a.(i+1) then begin ...
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[...
Rewrite the snippet below in C++ so it works the same as the original OCaml code.
let swap a i j = let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp; ;; let cocktail_sort a = let begin_ = ref(-1) and end_ = ref(Array.length a - 2) in let swapped = ref true in try while !swapped do swapped := false; incr begin_; for i = !begin_ to !end_ do if a.(i) > a.(i+1) then begin ...
#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], ...
Port the following code from OCaml to Java with equivalent syntax and logic.
let swap a i j = let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp; ;; let cocktail_sort a = let begin_ = ref(-1) and end_ = ref(Array.length a - 2) in let swapped = ref true in try while !swapped do swapped := false; incr begin_; for i = !begin_ to !end_ do if a.(i) > a.(i+1) then begin ...
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= ...
Maintain the same structure and functionality when rewriting this code in Python.
let swap a i j = let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp; ;; let cocktail_sort a = let begin_ = ref(-1) and end_ = ref(Array.length a - 2) in let swapped = ref true in try while !swapped do swapped := false; incr begin_; for i = !begin_ to !end_ do if a.(i) > a.(i+1) then begin ...
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: ...