Sorting algorithms/Insertion sort: Difference between revisions

Temporarily removed the Batch File solution
(Added P)
(Temporarily removed the Batch File solution)
Line 196:
1486 ; 9488 ; 9894 ; 17479 ; 18989 ; 23119 ; 23233 ; 24927 ; 25386 ; 26689 ;
</pre>
 
=={{header|Batch File}}==
Even though it is not required, I included an option to display each 'steps' in the sorting algorithm.
<lang dos>::Insertion Sort
::Batch File Implementation
 
@echo off
setlocal enabledelayedexpansion
 
::Initial Values and Variables
set "range=0"
set "output="
set "list=0 -1 5 2 7 4 3 6 -1 7 2 1 -2 8"
::Put the sequence of integers (ONLY) on the list variable.
::Please do not "play" with the list variable.
::or else the code will not work or crash.
 
set "displaystep=0"
::If you want the Batch file to display each number
::inserted and placed in its correct position, set this to 1.
::Else, just the sorted set will be displayed.
 
for %%l in (!list!) do (
set num!range!=%%l
set /a range+=1
)
set /a range-=1
 
set "enabl=rem " & set "disabl="
if !displaystep! equ 1 (
set "enabl="
set disabl=rem "
)
 
::Display initial condition
for /l %%l in (0,1,%range%) do set "output=!output! !num%%l!"
echo.Initial Sequence:
echo.
echo. ^>^> !output!
echo.
echo Sorting:
echo.
 
for /l %%i in (1,1,%range%) do (
set value=!num%%i!
set /a "last=%%i+1","j=%%i-1"
call :insert_loop
set /a res=j+1
set num!res!=!value!
%disabl%)
REM This is the output section.
set output=
for /l %%x in (0,1,%range%) do (if !last! equ %%x (
set "output=!output!^< !num%%x!"
) else (
set "output=!output! !num%%x!"
)
)
echo. ^>^> !output!
%enabl%)
 
::We are done.
echo DONE^^!
exit /b 0
 
::This is the 'while' loop...
:insert_loop
if !j! geq 0 (
if !num%j%! gtr !value! (
set /a res=j+1
set /a num!res!=num%j%
set /a j-=1
goto :insert_loop
)
)
goto :EOF</lang>
{{Out}}
'''Displaying the steps is disabled:'''
<pre>>Insertion.Bat
Initial Sequence:
 
>> 0 -1 5 2 7 4 3 6 -1 7 2 1 -2 8
 
Sorting:
 
>> -2 -1 -1 0 1 2 2 3 4 5 6 7 7 8
DONE!</pre>
'''Displaying the steps is enabled:'''
<pre>>Insertion.Bat
Initial Sequence:
 
>> 0 -1 5 2 7 4 3 6 -1 7 2 1 -2 8
 
Sorting:
 
>> -1 0< 5 2 7 4 3 6 -1 7 2 1 -2 8
>> -1 0 5< 2 7 4 3 6 -1 7 2 1 -2 8
>> -1 0 2 5< 7 4 3 6 -1 7 2 1 -2 8
>> -1 0 2 5 7< 4 3 6 -1 7 2 1 -2 8
>> -1 0 2 4 5 7< 3 6 -1 7 2 1 -2 8
>> -1 0 2 3 4 5 7< 6 -1 7 2 1 -2 8
>> -1 0 2 3 4 5 6 7< -1 7 2 1 -2 8
>> -1 -1 0 2 3 4 5 6 7< 7 2 1 -2 8
>> -1 -1 0 2 3 4 5 6 7 7< 2 1 -2 8
>> -1 -1 0 2 2 3 4 5 6 7 7< 1 -2 8
>> -1 -1 0 1 2 2 3 4 5 6 7 7< -2 8
>> -2 -1 -1 0 1 2 2 3 4 5 6 7 7< 8
>> -2 -1 -1 0 1 2 2 3 4 5 6 7 7 8
DONE!</pre>
 
=={{header|BBC BASIC}}==
535

edits