China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-26 23:04
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] Constructing an array in batch processing to achieve ordered arrangement of numbers View 3,606 Replies 7
Original Poster Posted 2006-11-15 00:16 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Suddenly I want to use batch processing to sort numbers. Previously, we compared several numbers respectively and arranged them in order, but when the number of numbers is small, if the number exceeds 10, the code will be very lengthy and the efficiency is relatively low.

If batch processing had the concept of array types like high-level languages, it would be great, but unfortunately. In batch processing, manually constructing an array can solve this problem.

So I wrote the following code, which can sort N numbers in descending or ascending order. Just change "lss" or "gtr" in the IF statement.


@echo off

setlocal enabledelayedexpansion

set num=0

:input

set /a num+=1

cls

set /p num%num%=Please enter a group of numbers, terminate by directly pressing Enter :

if not "!num%num%!"=="" goto input

set /a num-=1

set /a num_=num-1

cls

echo Former sort:

for /l %%k in (1,1,%num%) do (

set /p ii=!num%%k! <nul

)

for /l %%i in (1,1,%num_%) do (

set /a n=num-%%i

for /l %%j in (1,1,!n!) do (

set x=%%j

set /a y=x+1

call :go
)
)

echo.

echo.

echo Sorted after:

for /l %%k in (1,1,%num%) do (

set /p ii=!num%%k! <nul

)

echo.

pause >nul

goto :eof

:go

if !num%x%! gtr !num%y%! (

set i=!num%x%!

set num%x%=!num%y%!

set num%y%=!i!

)

goto :eof



Test code:


@echo off

setlocal enabledelayedexpansion

for /l %%l in (1,1,10) do (

set t=%%l

call :start

)

set /a num_=num-1

echo Former sort:

for /l %%k in (1,1,%num%) do (

set /p ii=!num%%k! <nul

)

for /l %%i in (1,1,%num_%) do (

set /a n=num-%%i

for /l %%j in (1,1,!n!) do (

set x=%%j

set /a y=x+1

call :go
)
)

echo.

echo.

echo Sorted after:

for /l %%k in (1,1,%num%) do (

set /p ii=!num%%k! <nul

)

echo.

pause >nul

goto :eof

:go

if !num%x%! lss !num%y%! (

set i=!num%x%!

set num%x%=!num%y%!

set num%y%=!i!

)

goto :eof

:start

set j=!RANDOM:~0,1!

set num%t%=!RANDOM:~0,%j%!

set /a num+=1

goto :eof


[ Last edited by pengfei on 2006-11-15 at 06:54 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +2 2006-11-15 00:38
Floor 2 Posted 2006-11-15 00:43 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-11-15 01:28 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
You can first determine the number of bits, which should be more efficient.
Floor 4 Posted 2006-11-15 01:32 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
RE: vkill

The code not only has the function of judging the number of bits, but also has precise control over the number of times the loop runs, and the execution efficiency is relatively ideal.

[ Last edited by pengfei on 2006-11-20 at 08:48 AM ]
业精于勤而荒于嬉,形成于思而毁于随。
Floor 5 Posted 2006-11-15 01:39 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Nice
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 6 Posted 2006-11-15 08:11 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Let's take a look at the test code of our "genius" moderator (it seems like 37.....moderator):

@echo off
setlocal enabledelayedexpansion
set numbers=%random% %random% %random% %random% %random% %random% %random% %random%
echo The array before sorting is: %numbers%
call :sort %numbers%
echo The array after sorting is: %ret%
pause
goto :eof

:sort
set num=%1
set str=%*
for %%i in (%str%) do if !num! gtr %%i set num=%%i
set ret=%ret% %num%
if not "!str:%num%=!" == "" call :sort !str:%num%=!
goto :eof

Is it more efficient! For decimal/

The following is the "applied" code improved from the above code:
@echo off
color 1f
if "%1"=="" goto :instruction
setlocal enabledelayedexpansion
for %%i in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do (
if not "%%i"=="" set number=!number! %%i)
call :sort %number%
echo.
echo The above numbers are in descending order as follows:
echo.
echo %str%
pause>nul

:instruction
cls
echo.
echo Number Sorting
echo.
echo ============================================================
echo Instructions:
echo This program can only be said to basically meet the requirements; no filtering of numbers;
echo Currently only supports sorting of up to 9 numbers!
echo.
echo Usage is as follows: [%~nx0] [%%1] [%%2] [%%3] [%%4] ........
echo =============================================================
echo.
cmd /k

:sort
set var=%1
set var_=%*
for %%i in (%var_%) do (
if !var! leq %%i set var=%%i)
set str=!str! !var!
set var_=!var_:%var%=!
if not "%var_%"=="" call :sort %var_%
Floor 7 Posted 2006-11-15 08:40 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Re youxi01:
To solve the limitation of 9 parameters, you can try using shift.
Of course, a better method is to replace the for statement under setlocal enabledelayedexpansion in your code with: set "number=%*"
Floor 8 Posted 2006-11-15 09:40 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
After looking at the code of moderator 3742668, I am really impressed...

Obviously, this method of constructing an array by arranging numbers with spaces in between is simpler. Then, read each array element one by one through the for /f command for comparison.

Not only is it faster, but the code is also concise. I have learned something...
业精于勤而荒于嬉,形成于思而毁于随。
Forum Jump: