How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the BASIC 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 BASIC programming language
Source code in the basic programming language
RANDOMIZE TIMER
DIM nums(9) AS INTEGER
DIM L0 AS INTEGER, L1 AS INTEGER, n AS INTEGER
'initial values
FOR L0 = 0 TO 9
nums(L0) = L0
NEXT
'scramble
FOR L0 = 9 TO 1 STEP -1
n = INT(RND * (L0)) + 1
IF n <> L0 THEN SWAP nums(n), nums(L0)
NEXT
'display initial condition
FOR L0 = 0 TO 9
PRINT nums(L0);
NEXT
PRINT
FOR L1 = 9 TO 1 STEP -1
n = 0
FOR L0 = 1 TO L1
IF nums(n) < nums(L0) THEN n = L0
NEXT
IF (n < L1) THEN
IF (n > 0) THEN
FOR L0 = 0 TO (n \ 2)
SWAP nums(L0), nums(n - L0)
NEXT
FOR L0 = 0 TO 9
PRINT nums(L0);
NEXT
PRINT
END IF
FOR L0 = 0 TO (L1 \ 2)
SWAP nums(L0), nums(L1 - L0)
NEXT
FOR L0 = 0 TO 9
PRINT nums(L0);
NEXT
PRINT
END IF
NEXT
RANDOMIZE TIMER
CONST delay = .1 'controls display speed
DIM nums(14) AS INTEGER
DIM L0 AS INTEGER, L1 AS INTEGER, n AS INTEGER, ttmp AS SINGLE
'initial values
FOR L0 = 0 TO 14
nums(L0) = L0
NEXT
'scramble
FOR L0 = 14 TO 1 STEP -1
n = INT(RND * (L0)) + 1
IF n <> L0 THEN SWAP nums(n), nums(L0)
NEXT
'display initial condition
CLS
GOSUB displayer
FOR L1 = 14 TO 1 STEP -1
n = 0
FOR L0 = 1 TO L1
IF nums(n) < nums(L0) THEN n = L0
NEXT
IF (n < L1) THEN
IF (n > 0) THEN
FOR L0 = 0 TO (n \ 2)
SWAP nums(L0), nums(n - L0)
NEXT
GOSUB displayer
END IF
FOR L0 = 0 TO (L1 \ 2)
SWAP nums(L0), nums(L1 - L0)
NEXT
GOSUB displayer
END IF
NEXT
COLOR 7
END
displayer:
LOCATE 1, 1
FOR L0 = 0 TO 14
COLOR nums(L0) + 1
IF nums(L0) < 10 THEN PRINT " ";
PRINT RTRIM$(LTRIM$(STR$(nums(L0)))); STRING$(nums(L0), 219); SPACE$(20)
NEXT
ttmp = TIMER
DO WHILE TIMER < ttmp + delay: LOOP
RETURN
You may also check:How to resolve the algorithm Day of the week step by step in the Ruby programming language
You may also check:How to resolve the algorithm Environment variables step by step in the Scala programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Pick random element step by step in the C# programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the 11l programming language