Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Transform the following OCaml implementation into VB, maintaining the same output 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 ...
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...
Port the following code from OCaml to Go 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 ...
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 Perl function in C with identical behavior.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
#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 Perl function in C# with identical behavior.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
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[...
Convert this Perl snippet to C++ and keep its semantics consistent.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
#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 Perl code.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
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.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
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: ...
Please provide an equivalent version of this Perl code in VB.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
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...
Preserve the algorithm and functionality while converting the code from Perl to Go.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
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] > ...
Convert the following code from PowerShell to C, ensuring the logic remains intact.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
#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 the following code from PowerShell to C#, ensuring the logic remains intact.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
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[...
Ensure the translated C++ code behaves exactly like the original PowerShell snippet.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
#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 PowerShell to Java without modifying what it does.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
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 PowerShell function in Python with identical behavior.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
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 PowerShell code into VB without altering its purpose.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
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 PowerShell code snippet into Go without altering its behavior.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
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 Racket code into C while preserving the original functionality.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
#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 Racket code into C# without altering its purpose.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
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[...
Ensure the translated C++ code behaves exactly like the original Racket snippet.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
#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], ...
Preserve the algorithm and functionality while converting the code from Racket to Java.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
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.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
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 Racket snippet without changing its computational steps.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
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...
Maintain the same structure and functionality when rewriting this code in Go.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
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] > ...
Preserve the algorithm and functionality while converting the code from COBOL to C.
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
#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...
Maintain the same structure and functionality when rewriting this code in C#.
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
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[...
Convert this COBOL snippet to C++ and keep its semantics consistent.
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
#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], ...
Can you help me rewrite this code in Java instead of COBOL, keeping it the same logically?
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
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= ...
Ensure the translated Python code behaves exactly like the original COBOL snippet.
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
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 COBOL implementation into VB, maintaining the same output and logic.
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
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...
Keep all operations the same but rewrite the snippet in Go.
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
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] > ...
Convert this REXX block to C, preserving its control flow and logic.
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
#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 the following code from REXX to C#, ensuring the logic remains intact.
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
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 REXX version.
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
#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 REXX to Java, same semantics.
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
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 REXX implementation.
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
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.
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
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 following REXX code into Go without altering its purpose.
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
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] > ...
Please provide an equivalent version of this Ruby code in C.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
#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 Ruby to C# with equivalent syntax and logic.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
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[...
Convert the following code from Ruby to C++, ensuring the logic remains intact.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
#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], ...
Please provide an equivalent version of this Ruby code in Java.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
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= ...
Port the following code from Ruby to Python with equivalent syntax and logic.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
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: ...
Produce a language-to-language conversion: from Ruby to VB, same semantics.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
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 Ruby code.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
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 Scala to C without modifying what it does.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = 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...
Maintain the same structure and functionality when rewriting this code in C#.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = 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[...
Convert this Scala snippet to C++ and keep its semantics consistent.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = 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], ...
Preserve the algorithm and functionality while converting the code from Scala to Java.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = 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= ...
Rewrite the snippet below in Python so it works the same as the original Scala code.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = 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: ...
Port the provided Scala code into VB while preserving the original functionality.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = 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...
Transform the following Scala implementation into Go, maintaining the same output and logic.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = 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] > ...
Translate the given Swift code snippet into C without altering its behavior.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
#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 Swift to C# without modifying what it does.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
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[...
Write the same algorithm in C++ as shown in this Swift implementation.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
#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], ...
Rewrite this program in Java while keeping its functionality equivalent to the Swift version.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
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= ...
Translate the given Swift code snippet into Python without altering its behavior.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
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 Swift snippet to VB and keep its semantics consistent.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
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 Swift function in Go with identical behavior.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
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] > ...
Translate this program into C but keep the logic exactly as in Tcl.
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
#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 Tcl snippet to C# and keep its semantics consistent.
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
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[...
Change the programming language of this snippet from Tcl to C++ without modifying what it does.
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
#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 provided Tcl code into Java while preserving the original functionality.
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
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 this program in Python while keeping its functionality equivalent to the Tcl version.
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
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 programming language of this snippet from Tcl to VB without modifying what it does.
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
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...
Keep all operations the same but rewrite the snippet in Go.
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
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] > ...
Convert this Rust block to PHP, preserving its control flow and logic.
fn cocktail_sort<T: PartialOrd>(a: &mut [T]) { let len = a.len(); loop { let mut swapped = false; let mut i = 0; while i + 1 < len { if a[i] > a[i + 1] { a.swap(i, i + 1); swapped = true; } i += 1; } if s...
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; } } } ...
Rewrite this program in PHP while keeping its functionality equivalent to the Ada version.
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...
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; } } } ...
Convert this Arturo snippet to PHP and keep its semantics consistent.
trySwap: function [arr,i][ if arr\[i] < arr\[i-1] [ tmp: arr\[i] arr\[i]: arr\[i-1] arr\[i-1]: tmp return null ] return true ] cocktailSort: function [items][ t: false l: size items while [not? t][ t: true loop 1..dec l 'i [ if null? tr...
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; } } } ...
Generate an equivalent PHP version of this AutoHotKey code.
MsgBox % CocktailSort("") MsgBox % CocktailSort("xxx") MsgBox % CocktailSort("3,2,1") MsgBox % CocktailSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z") CocktailSort(var) {   StringSplit array, var, `,   i0 := 1, i1 := array0   Loop { ...
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; } } } ...
Preserve the algorithm and functionality while converting the code from AWK to PHP.
{ line[NR] = $0 } END { swapped = 0 do { for(i=1; i < NR; i++) { if ( line[i] > line[i+1] ) { t = line[i] line[i] = line[i+1] line[i+1] = t swapped = 1 } } if ( swapped == 0 ) break swapped = 0 for(i=NR-1; i >= 1; i--) { if ( line[i] > line[i+1] ) { t = line[i] line[i]...
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; } } } ...
Produce a functionally identical PHP code for the snippet given in Common_Lisp.
(defun cocktail-sort-vector (vector predicate &aux (len (length vector))) (labels ((scan (start step &aux swapped) (loop for i = start then (+ i step) while (< 0 i len) do (when (funcall predicate (aref vector i) (aref vector (1- i))) ...
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; } } } ...
Maintain the same structure and functionality when rewriting this code in PHP.
module rosettaCode.sortingAlgorithms.cocktailSort; import std.range; Range cocktailSort(Range)(Range data) if (isRandomAccessRange!Range && hasLvalueElements!Range) { import std.algorithm : swap; bool swapped = void; void trySwap(E)(ref E lhs, ref E rhs) { if (lhs > rhs) { swap(lhs, ...
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; } } } ...
Rewrite this program in PHP while keeping its functionality equivalent to the Delphi version.
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 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; } } } ...
Generate an equivalent PHP version of this 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...
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 Factor code into PHP while preserving the original functionality.
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 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; } } } ...
Rewrite this program in PHP while keeping its functionality equivalent to the Forth version.
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 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; } } } ...
Generate an equivalent PHP version of this Fortran code.
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 PHP 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)...
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; } } } ...
Change the programming language of this snippet from Haskell to PHP without modifying what it does.
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 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; } } } ...
Rewrite this program in PHP while keeping its functionality equivalent to the Icon version.
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 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; } } } ...
Convert the following code from J to PHP, ensuring the logic remains intact.
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
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; } } } ...
Generate an equivalent PHP version of this Julia code.
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 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; } } } ...
Translate this program into PHP but keep the logic exactly as in Lua.
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 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; } } } ...
Rewrite this program in PHP while keeping its functionality equivalent to the Mathematica version.
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 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; } } } ...
Change the following MATLAB code into PHP without altering its purpose.
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 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; } } } ...
Generate a PHP translation of this Nim snippet without changing its computational steps.
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 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 OCaml code into PHP while preserving the original functionality.
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 ...
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; } } } ...
Produce a functionally identical PHP code for the snippet given in Perl.
use strict; use warnings; use feature 'say'; sub cocktail_sort { my @a = @_; while (1) { my $swapped_forward = 0; for my $i (0..$ if ($a[$i] gt $a[$i+1]) { @a[$i, $i+1] = @a[$i+1, $i]; $swapped_forward = 1; } } last if not ...
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; } } } ...
Produce a functionally identical PHP code for the snippet given in PowerShell.
function CocktailSort ($a) { $l = $a.Length $m = 0 if( $l -gt 1 ) { $hasChanged = $true :outer while ($hasChanged) { $hasChanged = $false $l-- for ($i = $m; $i -lt $l; $i++) { if ($a[$i] -gt $a[$i+1]) { $a[$i], $a[$i+1] = $a[$i+1], $a[$i] $hasChanged = $true } } if(-not $hasC...
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; } } } ...
Transform the following Racket implementation into PHP, maintaining the same output and logic.
#lang racket (require (only-in srfi/43 vector-swap!)) (define (cocktail-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap i j) (vector-swap! xs i j)) (define len (vector-length xs)) (define (bubble from to delta) (for/fold ([swaps 0]) ([i (in-range from to delta)]) (cond [(> (ref i) (ref (+ ...
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; } } } ...
Translate this program into PHP but keep the logic exactly as in COBOL.
C-SORT SECTION. C-000. DISPLAY "SORT STARTING". MOVE 2 TO WC-START MOVE WC-SIZE TO WC-END. MOVE 1 TO WC-DIRECTION WC-LAST-CHANGE. PERFORM E-SHAKER UNTIL WC-END * WC-DIRECTION < ...
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; } } } ...
Can you help me rewrite this code in PHP instead of REXX, keeping it the same logically?
options replace format comments java crossref savelog symbols binary placesList = [String - "UK London", "US New York" - , "US Boston", "US Washington" - , "UK Washington", "US Birmingham" - , "UK Birmingham", "UK Boston" - ] sortedList = cocktailSort(String[] Arrays.copyOf(placesList,...
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; } } } ...
Convert this Ruby snippet to PHP and keep its semantics consistent.
class Array def cocktailsort! begin swapped = false 0.upto(length - 2) do |i| if self[i] > self[i + 1] self[i], self[i + 1] = self[i + 1], self[i] swapped = true end end break unless swapped swapped = false (length - 2).downto(0) do |i...
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; } } } ...
Generate an equivalent PHP version of this Scala code.
fun cocktailSort(a: IntArray) { fun swap(i: Int, j: Int) { val temp = a[i] a[i] = a[j] a[j] = temp } do { var swapped = false for (i in 0 until a.size - 1) if (a[i] > a[i + 1]) { swap(i, i + 1) swapped = true ...
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 Swift code into PHP while preserving the original functionality.
extension Collection where Element: Comparable { public func cocktailSorted() -> [Element] { var swapped = false var ret = Array(self) guard count > 1 else { return ret } repeat { for i in 0...ret.count-2 where ret[i] > ret[i + 1] { (ret[i], ret[i + 1]) = (ret[i + 1], ret...
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; } } } ...
Can you help me rewrite this code in PHP instead of Tcl, keeping it the same logically?
package require Tcl 8.5 package require struct::list proc cocktailsort {A} { set len [llength $A] set swapped true while {$swapped} { set swapped false for {set i 0} {$i < $len - 1} {incr i} { set j [expr {$i + 1}] if {[lindex $A $i] > [lindex $A $j]} { ...
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; } } } ...
Produce a language-to-language conversion: from C to Rust, same semantics.
#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...
fn cocktail_sort<T: PartialOrd>(a: &mut [T]) { let len = a.len(); loop { let mut swapped = false; let mut i = 0; while i + 1 < len { if a[i] > a[i + 1] { a.swap(i, i + 1); swapped = true; } i += 1; } if s...
Translate the given C# code snippet into Rust without altering its behavior.
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[...
fn cocktail_sort<T: PartialOrd>(a: &mut [T]) { let len = a.len(); loop { let mut swapped = false; let mut i = 0; while i + 1 < len { if a[i] > a[i + 1] { a.swap(i, i + 1); swapped = true; } i += 1; } if s...
Write the same code in Rust as shown below in 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= ...
fn cocktail_sort<T: PartialOrd>(a: &mut [T]) { let len = a.len(); loop { let mut swapped = false; let mut i = 0; while i + 1 < len { if a[i] > a[i + 1] { a.swap(i, i + 1); swapped = true; } i += 1; } if s...
Port the provided Go code into Rust while preserving the original functionality.
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] > ...
fn cocktail_sort<T: PartialOrd>(a: &mut [T]) { let len = a.len(); loop { let mut swapped = false; let mut i = 0; while i + 1 < len { if a[i] > a[i + 1] { a.swap(i, i + 1); swapped = true; } i += 1; } if s...