(抱歉一切都是代码形式,堆栈溢出不会让我提交原因)
我最近对计算机排序感兴趣,并试图编写一个简单的批量数字排序器。它使用非常基本的数学并且效率极低,但主要是学习经验。该程序几乎适用于某些数字集。我对批处理比较陌生,所以据我所知,我的排序和检查没有什么特别的错误,但我在排序后遇到了不少异常现象。在一组10个值中,它完美排序,除了最大数量最终被放在第一位。在较大的数字集中,例如:
> Values:10
>
> Random Maximum:10
>
> UNSORTED: 8 5 0 0 5 4 7 1 5 1
>
> SORTED: 8 0 1 1 4 5 5 5 7
以下是我在调试时遇到的异常情况:
排在最前面的数字
随机放置在较低位置的数字(数十,数百等)大多数数字较高的
从未排序的
在sorted.txt
可能会发现更多
到目前为止,这是我的批处理脚本:
@echo off
color b
title sorting
if exist sortedlist.txt del sortedlist.txt
if exist unsortedlist.txt del unsortedlist.txt
if exist log.txt del log.txt
setlocal enabledelayedexpansion
set ar=0
set num=0
set check=0
set checknum=0
set checks=0
set /P totalnumber="Values:"
set /P randmax="Max Random Value:"
cls
::set totalnumber=200
::set randmax=500
echo %date% : %time%
echo PROGRAM INITIATED
echo VALUES:%totalnumber%
echo RANDMAX:%randmax%
echo %date% : %time% >> log.txt
echo PROGRAM INITIATED >> log.txt
echo VALUES:%totalnumber% >> log.txt
echo RANDMAX:%randmax% >> log.txt
:array
if %num% LSS %totalnumber% (
set /A a[%num%]=%random% %% %randmax%
set /A num=%num%+1
goto array
)
if %num% EQU %totalnumber% (
echo %DATE%:%TIME%
echo VALUE GEN COMPLETE
echo %date%:%time% >> log.txt
echo VALUE GEN COMPLETE >> log.txt
goto echo1
)
:echo1
for /F "tokens=2 delims==" %%s in ('set a[') do echo %%s
for /F "tokens=2 delims==" %%s in ('set a[') do echo %%s >> unsortedlist.txt
echo sort initialized
goto sort
)
:sort
set sorts=0
set n=0
:sortloop
set /A m=%n%+1
if %n% EQU %totalnumber% (
echo %date% : %time%
echo SORT COMPLETED
echo SORTS : %sorts%
echo %date% : %time% >> log.txt
echo SORT COMPLETED >> log.txt
echo SORTS : %sorts% >> log.txt
goto check
)
if !a[%n%]! GTR !a[%m%]! (
set hold=!a[%m%]!
set a[%m%]=!a[%n%]!
set a[%n%]=%hold%
set /A n=%n%+1
set /A sorts=%sorts%+1
goto sortloop
)
if !a[%n%]! LSS !a[%m%]! (
set /A n=%n%+1
goto sortloop
)
if !a[%n%]! EQU !a[%m%]! (
set /A n=%n%+1
goto sortloop
)
pause
:check
set check=0
set checknum=0
set checks=0
:checkloop
set /A checknumplus=%checknum%+1
if %check% EQU %totalnumber% (
goto complete
)
if %checknum% EQU %totalnumber% (
set /A checks=%checks%+1
echo %date% : %time%
echo CORRECT PAIRS : %check%
echo CHECKS : %checks%
echo %date% : %time% >> log.txt
echo CORRECT PAIRS : %check% >> log.txt
echo CHECKS : %checks% >> log.txt
goto sort
)
if !a[%checknum%]! LSS !a[%checknumplus%]! (
set /A check=%check%+1
set /A checknum=%checknum%+1
goto checkloop
)
if !a[%checknum%]! GTR !a[%checknumplus%]! (
set /A checknum=%checknum%+1
goto checkloop
)
if !a[%checknum%]! EQU !a[%checknumplus%]! (
set /A check=%check%+1
set /A checknum=%checknum%+1
goto checkloop
)
pause
:complete
set a[%totalnumber%+1]=%a[0]%
set %a[0]%=0
echo %date% : %time%
echo PROGRAM COMPLETE
echo %date% : %time% >> log.txt
echo PROGRAM COMPLETE >> log.txt
for /F "tokens=2 delims==" %%s in ('set a[') do echo %%s
for /F "tokens=2 delims==" %%s in ('set a[') do echo %%s >> sortedlist.txt
任何帮助清理代码,添加代码或解决我遇到的任何问题都将不胜感激。
答案 0 :(得分:1)
这个问题在"有趣"。无论您对批处理文件的了解程度如何,您都没有就发布的代码提出一个明确的问题!你基本上邀请我们做任何我们想做的事情,这就是我所做的。此外,重要的是要注意所谓的"输出示例"在问题中显示与发布的代码无关......
编辑:用于在一行中显示数字列表的方法使用变量,因此最大结果数受最大可变长度的限制。例如,每个数字为4位,列表最多可包含1637个数字,恕我直言,这对于简单的批量数字分拣机来说已足够了#34;程序
我修改了用于显示结果的方法,因此它现在支持10,000个随机数或更多。但是,您应该注意此排序方法是慢,因此我建议在sortLoop中插入echo
命令,以便知道程序仍在运行。
@echo off
setlocal EnableDelayedExpansion
color b
title sorting
set /P "totalNumber=Values:"
set /P "randMax=Max Random Value:"
echo %date% : %time%
echo PROGRAM INITIATED
echo VALUES:%totalNumber%
echo randMax:%randMax%
(
echo %date% : %time%
echo PROGRAM INITIATED
echo VALUES:%totalNumber%
echo randMax:%randMax%
) > log.txt
:array
for /L %%i in (1,1,%totalNumber%) do set /A a[%%i]=!random! %% randMax
echo %DATE%:%TIME%
echo VALUE GEN COMPLETE
(
echo %date%:%time%
echo VALUE GEN COMPLETE
) >> log.txt
echo/
< NUL (
set /P "=UNSORTED: "
for /L %%i in (1,1,%totalNumber%) do set /P "=!a[%%i]! "
)
echo/
echo/
echo sort initialized
set "sorts=0"
:sortLoop
set "hold="
for /L %%m in (2,1,%totalNumber%) do (
set /A n=%%m-1
for %%n in (!n!) do if !a[%%n]! GTR !a[%%m]! (
set /A hold=a[%%m], a[%%m]=a[%%n], a[%%n]=hold, sorts+=1
)
)
REM ECHO Sorts: %sorts%
if defined hold goto sortLoop
echo %date% : %time%
echo SORT COMPLETED
echo SORTS : %sorts%
(
echo %date% : %time%
echo SORT COMPLETED
echo SORTS : %sorts%
) >> log.txt
:complete
echo %date% : %time%
echo PROGRAM COMPLETE
(
echo %date% : %time%
echo PROGRAM COMPLETE
) >> log.txt
echo/
< NUL (
set /P "=SORTED: "
for /L %%i in (1,1,%totalNumber%) do set /P "=!a[%%i]! "
)
echo/
编辑:添加了新的排序方法
我原本以为这个程序代表了你,而且主要是计算机排序的学习经历&#34;。但是,如果您有兴趣以有效的方式创建名为sorted.txt
和unsorted.txt
的随机数的两个文件,则可以改为使用此代码:
@echo off
setlocal EnableDelayedExpansion
set /P "totalNumber=Values:"
set /P "randMax=Max Random Value:"
echo/
set /P "=UNSORTED: " < NUL
< NUL (for /L %%i in (1,1,%totalNumber%) do (
set /A a=!random! %% randMax
set /P "=!a! " > CON
set "a= !a!"
echo !a:~-5!
)) > unsorted.txt
echo/
sort unsorted.txt /O sorted.txt
echo/
< NUL (
set /P "=SORTED: "
for /F %%a in (sorted.txt) do set /P "=%%a "
)
echo/
请记住,最大随机数为32767,但数量没有限制。
答案 1 :(得分:0)
AS aacini提到的set将按字母顺序排序环境变量,将数字排序为前导零并将随机数放入var名称,以便能够多次存储相同的数字附加索引。
编辑修改批次以将列表输出到unsorted.txt
和sorted.txt
@echo off
setlocal EnableDelayedExpansion
set /P "totalNumber=Number of Values:"
set /P "randMax=Max Random Value:"
echo %date% : %time% PROGRAM INITIATED VALUES:%totalNumber% randMax:%randMax%
(echo %date% : %time% PROGRAM INITIATED VALUES:%totalNumber% randMax:%randMax%) > log.txt
:array
(for /L %%i in (1,1,%totalNumber%) do (
set /A "Rno=!random! %% randMax + 10*randMax"
Set a_!Rno!_%%i=0
Echo:!Rno!
))> unsorted.txt
echo/
echo %date% : %time% Generated %totalNumber% numbers
(echo %date% : %time% Generated %totalNumber% numbers) >> log.txt
(for /F "tokens=2 delims=_" %%i in ('set a_') do (
Set /A No=%%i - 10*randMax,Diff=No-Prev,Prev=No
Echo:!No!
))> sorted.txt
:complete
echo %date% : %time% PROGRAM COMPLETE
(echo %date% : %time% PROGRAM COMPLETE) >> log.txt