How to resolve the algorithm Sorting algorithms/Comb sort step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Comb sort step by step in the PL/I programming language

Table of Contents

Problem Statement

Implement a   comb sort.

The Comb Sort is a variant of the Bubble Sort. Like the Shell sort, the Comb Sort increases the gap used in comparisons and exchanges. Dividing the gap by

( 1 −

e

− φ

)

− 1

≈ 1.247330950103979

{\displaystyle (1-e^{-\varphi })^{-1}\approx 1.247330950103979}

works best, but   1.3   may be more practical.

Some implementations use the insertion sort once the gap is less than a certain amount.

Variants:

Pseudocode:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Comb sort step by step in the PL/I programming language

Source code in the pl/i programming language

/* From the pseudocode. */
comb_sort: procedure (A);
   declare A(*) fixed;
   declare t fixed;
   declare (i, gap) fixed binary (31);
   declare swaps bit (1) aligned;

   gap = hbound(A,1) - lbound(A,1);  /* initialize the gap size. */
   do until (gap <= 1 & swaps);
      /* update the gap value for a next comb. */
      put skip data (gap);
      gap = gap / 1.25e0;
      put skip data (gap);
      swaps = '1'b;
      /* a single "comb" over the array. */
      do i = lbound(A,1) by 1 until (i + gap >= hbound(A,1));
         if A(i) > A(i+gap) then
            do;
               t = A(i); A(i) = A(i+gap); A(i+gap) = t;
               swaps = '0'b; /* Flag a swap has occurred, so */
                             /* the list is not guaranteed sorted. */
            end;
       end;
   end;
end comb_sort;

  

You may also check:How to resolve the algorithm Prime triangle step by step in the C programming language
You may also check:How to resolve the algorithm Sort a list of object identifiers step by step in the Raku programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the C# programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the D programming language