How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Batch File programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Batch File 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 Batch File programming language
Source code in the batch programming language
:: Pancake Sort from Rosetta Code
:: Batch File Implementation
@echo off
setlocal enabledelayedexpansion
:: put the input sequence of integers (only) on the list variable.
set "list=-2 0 -1 5 2 7 4 3 6 -1 7 2 1 8"
:: create a pseudo-array; start at 0.
set "range=-1"
for %%l in (%list%) do (
set /a "range+=1"
set "num!range!=%%l"
)
:: scramble (remove this if you do not want to scramble the integers)
for /l %%l in (%range%,-1,1) do (
set /a "n=%random% %% %%l"
rem swapping...
for %%? in (num!n!) do set "swaptemp=!%%?!"
set "num!n!=!num%%l!"
set "num%%l=!swaptemp!"
)
:: display initial condition
set "output="
for /l %%l in (0,1,%range%) do set "output=!output! !num%%l!"
echo(Initial Sequence:
echo(
echo( ^>^> %output%
echo(
echo(Sorting:
echo(
:: begin sort
for /l %%l in (%range%,-1,1) do (
set "n=0"
for /l %%m in (1,1,%%l) do (
for %%? in (num!n!) do if !%%?! lss !num%%m! set "n=%%m"
)
if !n! lss %%l (
if !n! gtr 0 (
set /a "tempvar1=!n!/2" %== corresponds to (n \ 2) from BASIC code ==%
for /l %%m in (0,1,!tempvar1!) do (
set /a "tempvar2=!n!-%%m" %== corresponds to (n - L0) from BASIC code ==%
rem swapping...
for %%? in (num!tempvar2!) do set "swaptemp=!%%?!"
set "num!tempvar2!=!num%%m!"
set "num%%m=!swaptemp!"
)
rem display the action
set "output="
for /l %%x in (0,1,%range%) do set "output=!output! !num%%x!"
echo( ^>^> !output!
)
set /a "tempvar1=%%l/2" %== corresponds to (L1 \ 2) from BASIC code ==%
for /l %%m in (0,1,!tempvar1!) do (
set /a "tempvar2=%%l-%%m" %== corresponds to (L1 - L0) from BASIC code ==%
rem swapping...
for %%? in (num!tempvar2!) do set "swaptemp=!%%?!"
set "num!tempvar2!=!num%%m!"
set "num%%m=!swaptemp!"
)
rem display the action
set output=
for /l %%x in (0,1,%range%) do set "output=!output! !num%%x!"
echo. ^>^> !output!
)
)
)
echo DONE^^!
exit /b 0
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Logo programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the Mathematica/ Wolfram Language programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Distribution of 0 digits in factorial series step by step in the Perl programming language
You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Scheme programming language