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

Table of Contents

Problem Statement

Sort an array of integers (of any convenient size) into ascending order using Pancake sorting. In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so: Only one end of the list can be flipped; this should be the low end, but the high end is okay if it's easier to code or works better, but it must be the same end for the entire solution. (The end flipped can't be arbitrarily changed.) Show both the initial, unsorted list and the final sorted list. (Intermediate steps during sorting are optional.) Optimizations are optional (but recommended).

Let's start with the solution:

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

Source code in the pl/i programming language

pancake_sort: procedure options (main); /* 23 April 2009 */
   declare a(10) fixed, (i, n, loc) fixed binary;

   a(1) = 3; a(2) = 9; a(3) = 2; a(4) = 7; a(5) = 10;
   a(6) = 1; a(7) = 8; a(8) = 5; a(9) = 4; a(10) = 6;

   n = hbound(A,1);
   put skip edit (A) (f(5));
   do i = 1 to n-1;
      loc = max(A, n);
      call flip (A, loc);
      call flip (A, n);
      n = n - 1;
      put skip edit (A) (f(5));
   end;

max: procedure (A, k) returns (fixed binary);
   declare A(*) fixed, k fixed binary;
   declare (i, maximum, loc) fixed binary;
   maximum = A(1); loc = 1;
   do i = 2 to k;
      if A(i) > maximum then do; maximum = A(i); loc = i; end;
   end;
   return (loc);
end max;

flip: procedure (A, k);
   declare A(*) fixed, k fixed binary;
   declare (i, t) fixed binary;
   do i = 1 to (k+1)/2;
      t = A(i); A(i) = A(k-i+1); A(k-i+1) = t;
   end;
end flip;

end pancake_sort;

    3    9    2    7   10    1    8    5    4    6
    6    4    5    8    1    3    9    2    7   10
    7    2    6    4    5    8    1    3    9   10
    3    1    7    2    6    4    5    8    9   10
    5    4    6    2    3    1    7    8    9   10
    1    3    2    5    4    6    7    8    9   10
    4    1    3    2    5    6    7    8    9   10
    2    3    1    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10


  

You may also check:How to resolve the algorithm Pinstripe/Printer step by step in the Wren programming language
You may also check:How to resolve the algorithm Death Star step by step in the Brlcad programming language
You may also check:How to resolve the algorithm File input/output step by step in the 11l programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the E programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Modula-3 programming language