How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the REXX programming language

Table of Contents

Problem Statement

The   cocktail sort   is an improvement on the   Bubble Sort.

A cocktail sort is also known as:

The improvement is basically that values "bubble"   (migrate)   both directions through the array,   because on each iteration the cocktail sort   bubble sorts   once forwards and once backwards. After   ii   passes,   the first   ii   and the last   ii   elements in the array are in their correct positions,   and don't have to be checked (again). By shortening the part of the array that is sorted each time,   the number of comparisons can be halved.

Pseudocode for the   2nd   algorithm   (from Wikipedia)   with an added comment and changed indentations: %   indicates a comment,   and   deal   indicates a   swap.

Implement a   cocktail sort   and optionally show the sorted output here on this page. See the   discussion   page for some timing comparisons.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the REXX programming language

Source code in the rexx programming language

/*REXX program sorts an array using the   cocktail─sort   method  with shifting bounds. */
call gen                                         /*generate some array elements.        */
call show  'before sort'                         /*show  unsorted  array elements.      */
     say copies('█', 101)                        /*show a separator line  (a fence).    */
call cocktailSort  #                             /*invoke the cocktail sort subroutine. */
call show  ' after sort'                         /*show    sorted  array elements.      */
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cocktailSort: procedure expose @.;    parse arg N             /*N:  is number of items. */
                                      end$= N - 1;     beg$= 1
                     do while beg$ <= end$
                     beg$$= end$;               end$$= beg$
                        do j=beg$ to end$;                   jp= j + 1
                        if @.j>@.jp  then do;  _=@.j;  @.j=@.jp;  @.jp=_;  end$$=j;  end
                        end   /*j*/
                     end$= end$$ - 1
                        do k=end$  to beg$  by -1;           kp= k + 1
                        if @.k>@.kp  then do;  _=@.k;  @.k=@.kp;  @.kp=_;  beg$$=k;  end
                        end   /*k*/
                     beg$= beg$$ + 1
                     end      /*while*/
              return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=                                        /*assign a default value for the stem. */
     @.1 = '---the 22 card tarot deck (larger deck has 56 additional cards in 4 suits)---'
     @.2 = '==========symbol====================pip======================================'
     @.3 = 'the juggler                  ◄───     I'
     @.4 = 'the high priestess  [Popess] ◄───    II'
     @.5 = 'the empress                  ◄───   III'
     @.6 = 'the emperor                  ◄───    IV'
     @.7 = 'the hierophant  [Pope]       ◄───     V'
     @.8 = 'the lovers                   ◄───    VI'
     @.9 = 'the chariot                  ◄───   VII'
     @.10= 'justice                      ◄───  VIII'
     @.11= 'the hermit                   ◄───    IX'
     @.12= 'fortune  [the wheel of]      ◄───     X'
     @.13= 'strength                     ◄───    XI'
     @.14= 'the hanging man              ◄───   XII'
     @.15= 'death  [often unlabeled]     ◄───  XIII'
     @.16= 'temperance                   ◄───   XIV'
     @.17= 'the devil                    ◄───    XV'
     @.18= 'lightning  [the tower]       ◄───   XVI'
     @.18= 'the stars                    ◄───  XVII'
     @.20= 'the moon                     ◄─── XVIII'
     @.21= 'the sun                      ◄───   XIX'
     @.22= 'judgment                     ◄───    XX'
     @.23= 'the world                    ◄───   XXI'
     @.24= 'the fool  [often unnumbered] ◄───  XXII'

            do #= 1  until @.#==''; end;  #= #-1 /*find how many entries in the array.  */
     return                                      /* [↑]  adjust for DO loop advancement.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: w= length(#);               do j=1  for #      /*#:  is the number of items in @. */
                                  say 'element'    right(j, w)     arg(1)":"    @.j
                                  end   /*j*/        /*     ↑                           */
      return                                         /*     └─────max width of any line.*/


  

You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the Scala programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Haskell programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Empty string step by step in the BASIC programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the PARI/GP programming language