How to resolve the algorithm Sorting algorithms/Gnome 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/Gnome sort step by step in the PL/I programming language

Table of Contents

Problem Statement

Gnome sort is a sorting algorithm which is similar to Insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in Bubble Sort. The pseudocode for the algorithm is:

Implement the Gnome sort in your language to sort an array (or list) of numbers.

Let's start with the solution:

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

Source code in the pl/i programming language

SORT: PROCEDURE OPTIONS (MAIN);
   DECLARE A(0:9) FIXED STATIC INITIAL (5, 2, 7, 1, 9, 8, 6, 3, 4, 0);

   CALL GNOME_SORT (A);
   put skip edit (A) (f(7));

GNOME_SORT: PROCEDURE (A) OPTIONS (REORDER); /* 9 September 2015 */
   declare A(*) fixed;
   declare t fixed;
   declare (i, j) fixed;

   i = 1; j = 2;
   do while (i <= hbound(A));
      if a(i-1) <= a(i) then
         do;
            i = j; j = j + 1;
         end;
      else
         do;
            t = a(i-1); a(i-1) = a(i); a(i) = t;
            i = i - 1;
            if i = 0 then do; i = j; j = j + 1; end;
         end;
   end;

END GNOME_SORT;

END SORT;

  

You may also check:How to resolve the algorithm Text processing/2 step by step in the PL/I programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the PL/I programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the PL/I programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the PL/I programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the PL/I programming language