How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the uBasic/4tH programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the uBasic/4tH 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 uBasic/4tH programming language
Source code in the ubasic/4th programming language
PRINT "Pancake sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
PROC _Pancakesort (n)
PROC _ShowArray (n)
PRINT
END
_Flip PARAM(1)
LOCAL(1)
b@ = 0
DO WHILE b@ < a@
PROC _Swap (b@, a@)
b@ = b@ + 1
a@ = a@ - 1
LOOP
RETURN
_Pancakesort PARAM (1) ' Pancakesort
LOCAL(3)
IF a@ < 2 THEN RETURN
FOR b@ = a@ TO 2 STEP -1
c@ = 0
FOR d@ = 0 TO b@ - 1
IF @(d@) > @(c@) THEN c@ = d@
NEXT
IF c@ = b@ - 1 THEN CONTINUE
IF c@ THEN PROC _Flip (c@)
PROC _Flip (b@ - 1)
NEXT
RETURN
_Swap PARAM(2) ' Swap two array elements
PUSH @(a@)
@(a@) = @(b@)
@(b@) = POP()
RETURN
_InitArray ' Init example array
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
FOR i = 0 TO 9
@(i) = POP()
NEXT
RETURN (i)
_ShowArray PARAM (1) ' Show array subroutine
FOR i = 0 TO a@-1
PRINT @(i),
NEXT
PRINT
RETURN
You may also check:How to resolve the algorithm Perfect numbers step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Zhang-Suen thinning algorithm step by step in the Action! programming language
You may also check:How to resolve the algorithm Object serialization step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Euler's constant 0.5772... step by step in the Raku programming language
You may also check:How to resolve the algorithm Tic-tac-toe step by step in the Forth programming language