-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-5-bogosort-parameter.cmd
51 lines (40 loc) · 1.05 KB
/
3-5-bogosort-parameter.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@echo off & setlocal enableDelayedExpansion
if "%~1"=="" echo.Bitte die zu sortierenden Integer als Parameter uebergeben.&exit /B 1
goto main
:bogosort Array<int>
call :isSorted "%~1" && exit /B 0
call :randomElement n_0 "%~1"
call :randomElement n_1 "%~1"
set "tmp=!%~1[%n_0%]!"
set "%~1[%n_0%]=!%~1[%n_1%]!"
set "%~1[%n_1%]=%tmp%"
goto bogosort
:isSorted Array<int>
set _last=-Infinity
for /L %%i in (0 1 !%~1.lastIndex!) do (
if not "!_last!"=="-Infinity" (
REM zur Uenterstuetzung anderer Datentypen muss dieser Vergleich angepasst werden:
if not !_last! LEQ !%~1[%%i]! (
exit /B 1
)
)
set "_last=!%~1[%%i]!"
)
exit /B 0
:randomElement var Array<>
set /a "%~1 = !%~2.length! * %random% / 32768"
exit /B !%~1!
:print Array<>
for /L %%i in (0 1 !%~1.lastIndex!) do echo+|set /p "=!%~1[%%i]! "
exit /B
:main
set /a "list.length=0"
:saveArgs
set "list[%list.length%]=%~1"
set /a "list.length+=1"
shift
if not "%~1"=="" goto saveArgs
set /a "list.lastIndex=list.length-1"
call:bogosort list
call:print list
exit /B 0