How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the zkl programming language

Table of Contents

Problem Statement

The cocktail shaker sort is an improvement on the Bubble Sort. The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from wikipedia):

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the zkl programming language

Source code in the zkl programming language

fcn cocktailSort(a){
   swapped,begin,end:=False,-1,a.len() - 2;
   do{
      swapped,begin=False,begin + 1;
      foreach i in ([begin .. end]){
	 if(a[i]>a[i+1]){ a.swap(i,i+1); swapped=True; }
      }
      if(not swapped) break;
      swapped,end=False,end - 1;
      foreach i in ([end..begin,-1]){
         if(a[i]>a[i+1]){ a.swap(i,i+1); swapped=True; }
      }
   }while(swapped);
   a
}

cocktailSort(List(2,1)).println();
x:=List(5, -1, 101, -4, 0, 1, 8, 6, 2, 3 );
cocktailSort(x).println();
x="the lazy fox jumped over the brown dog".split(" ").copy();
cocktailSort(x).println();

  

You may also check:How to resolve the algorithm Playing cards step by step in the Quackery programming language
You may also check:How to resolve the algorithm Comments step by step in the OxygenBasic programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Picat programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the Kotlin programming language