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-22 21:34
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Numerical Sorting (Difficulty: Difficult, Problem Solved) DigestI View 12,670 Replies 31
Original Poster Posted 2008-04-29 22:22 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
If there is a column of values such as: 56 12 33 48 99 105 5 16
How to output it as: 5 12 16 33 48 56 99 105 through batch processing, and no temporary files are to be generated.
----------------------------------------------------------------------------------------------------------
Overall, my general code is as follows:

@echo off&setlocal enabledelayedexpansion
set "str=56 12 33 48 99 105 5 16"
for %%i in (%str%) do call,set var=!var! n%%in
for %%i in (%var%) do set a=%%i&call :lp
echo !var:n=!
pause>nul&goto :eof
:lp
for %%i in (%var%) do (
set b=%%i&set b=!b:~1,-1!&set c=%a:~1,-1%
if !b! lss !c! (
set var=!var:%a% =!
set var=!var:%%i=%%i %a%!
) else (
set var=!var!
)
)

Further expansion:

@echo off&setlocal enabledelayedexpansion
set "str=%random% %random% -%random% %random% %random% %random% -%random%"
for %%i in (%str%) do call,set var=!var! n%%in
for %%i in (%var%) do set a=%%i&call :lp
echo !var:n=!
pause>nul&goto :eof
:lp
for %%i in (%var%) do (
set b=%%i&set b=!b:~1,-1!&set c=%a:~1,-1%
if !b! lss !c! (
set var=!var:%a% =!
set var=!var:%%i=%%i %a%!
) else (
set var=!var!
)
)


[ Last edited by zw19750516 on 2008-5-4 at 09:20 PM ]
批处理之家新域名:www.bathome.net
Floor 2 Posted 2008-04-29 22:40 ·  中国 四川 德阳 移动
高级用户
★★★
Credits 502
Posts 327
Joined 2006-12-30 06:01
19-year member
UID 74981
Gender Male
Status Offline
Arrange them in order from small to large.
Floor 3 Posted 2008-04-29 22:44 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
If it's purely P, I usually first compare the lengths, and if the lengths are the same, then compare the values.
Floor 4 Posted 2008-04-29 23:21 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Linux

$ cat TEST
56
12
33
48
99
105
5
16

$ sort -n <TEST
5
12
16
33
48
56
99
105
Floor 5 Posted 2008-04-29 23:34 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
The moderator also comes to join in the fun...

cmd is not as powerful as shell.
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 6 Posted 2008-04-29 23:37 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
There is still no perfect answer so far!
批处理之家新域名:www.bathome.net
Floor 7 Posted 2008-04-29 23:56 ·  中国 江苏 常州 武进区 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
@echo off&setlocal enabledelayedexpansion
set/p str=Enter a number (separated by spaces)
for %%i in (%str%) do (
set str=0000000000%%i
set .!str:~-10! !random!=a
)
for /f "delims=.= " %%i in ('set .') do for /f "tokens=* delims=0" %%i in ("%%i") do if "%%i"=="" (set/p=0 <nul) else set/p=%%i <nul
pause>nul
Recent Ratings for This Post ( 4 in total) Click for details
RaterScoreTime
bat-zw +15 2008-04-30 00:16
huahua0919 +8 2008-04-30 17:13
PPdos +4 2008-05-02 06:40
plp626 +11 2008-05-04 22:00
简单!简单!再简单!
Floor 8 Posted 2008-04-29 23:59 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
```
@echo off&if not %1.==. goto%*
::plp@cn-dos.net|08-4-29
::Number sorting. Number range: (0-10^99) Numbers are saved in dat.txt, one number per line
if not exist dat.txt echo Missing dat.txt&pause&exit
for /f "delims= " %%a in ('"%0" :sor^|sort')do echo %%a
pause
:sor
setlocal enabledelayedexpansion
for /f %%a in (dat.txt)do (set d=%%a
call:byte&call:space
echo\!sp!!d!
)
exit/b
:byte
for /l %%z in (1 1 100)do if "!d:~%%z,1!"=="" set/a byte=100-%%z&goto:eof
:space
set "sp="
for /l %%z in (1 1 %byte%)do set "sp=!sp! "
goto:eof

Numbers are not one per line. First use this code to change it to one per line

@echo off
for /f "delims=" %%a in (test.txt)do call:dd %%a
exit
:dd
if %1.=. goto:eof
>>dat.txt echo %1
shift
goto:dd


[ Last edited by plp626 on 2008-4-30 at 01:41 PM ]
```
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 9 Posted 2008-04-30 00:01 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
I wonder if brother s11ss's sorting algorithm can meet the building owner's needs

''''''''      Sorting      ''''''''
''''''''s11ss 2007-10-18''''''''

option explicit
'receive input:
dim s,r,n,i
s=inputbox(vbcrlf&vbcrlf&"Separate with spaces:","Please enter a group of numbers:","2007 10 18 21 15")
if s="" then wscript.quit
r=split(s," ")
n=ubound(r)
'Convert string to Double subtype:
for i=0 to n
r(i)=cdbl(r(i))
next

'Call quick sort method:
quicksort r,0,n
'Call other sorting methods:
'insertsort r
'shellsort r
'bubblesort r
'selectsort r
'heapsort r

'Output result:
inputbox vbcrlf&vbcrlf&"In ascending order is:","Result",join(r," ")

'Customize various sorting subroutines:
'Direct insertion sort:
sub insertsort(r)
dim i,n,t,j
n=ubound(r)
for i=1 to n'Insert r(1), r(2),..., r(n) in turn
t=r(i)
j=i-1
do while t<r(j)'Find the insertion position of r(i)
r(j+1)=r(j)'Shift numbers larger than r(i) backward
j=j-1
if j=-1 then exit do
loop
r(j+1)=t'Insert r(i)
next
end sub

'Shell sort:
sub shellsort(r)
'Set increment sequence:
dim i,d(),n,t,k,h,j
n=ubound(r)
i=0
redim d(n)
d(i)=fix(n/2)
do until d(i)=1
t=d(i)
i=i+1
d(i)=fix(t/2)
loop
'Sort:
k=0
do
h=d(k)'Take the increment of this pass
for i=h to n'r(h) to r(n) insert into the current ordered area
t=r(i)'Save the number to be inserted
j=i-h
do while t<r(j)'Find the correct insertion position
r(j+h)=r(j)'Shift backward
j=j-h'Get the position of the previous number
if j<0 then exit do
loop
r(j+h)=t'Insert r(i)
next'Complete this pass of sorting
k=k+1
loop while h<>1
end sub

'Bubble sort:
sub bubblesort(r)
dim i,n,noswap,j,t
n=ubound(r)
for i=0 to n-1'do n passes of sorting
noswap=True'set unswapped flag
for j=n-1 to i step -1'scan from bottom up
if r(j+1)<r(j) then'exchange
t=r(j)
r(j)=r(j+1)
r(j+1)=t
noswap=False
end if
next
if noswap then exit for'terminate the algorithm if no swap occurs in this pass of sorting
next
end sub

'Quick sort:
'Partition:
function partition(r,l,h)
dim i,j,t
i=l
j=h
t=r(i)'Initialize, t is the pivot
do
while r(j)>=t and i<j
j=j-1'scan from right to left, find the first number less than t
wend
if i<j then
r(i)=r(j)'exchange r(i) and r(j)
i=i+1
end if
while r(i)<=t and i<j
i=i+1'scan from left to right, find the first number greater than t
wend
if i<j then
r(j)=r(i)'exchange r(i) and r(j)
j=j-1
end if
loop while i<>j
r(i)=t'the pivot t has been finally positioned
partition=i
end function
'Sort:
sub quicksort(r,s1,t1)
dim i
if s1<t1 then'no need to sort if there is one number or no number
i=partition(r,s1,t1)'partition r(s1) to r(t1)
quicksort r,s1,i-1'recursively process the left interval
quicksort r,i+1,t1'recursively process the right interval
end if
end sub

'Direct selection sort:
sub selectsort(r)
dim i,n,k,j,t
n=ubound(r)
for i=0 to n-1'do n passes of sorting
k=i
for j=i+1 to n'select the smallest number r(k) in the current unsorted area
if r(j)<r(k) then k=j
next
if k<>i then
t=r(i)
r(i)=r(k)
r(k)=t
end if
next
end sub

'Heap sort:
'Sift:
sub sift(r,i,m)'the complete binary tree with r(i) as the root forms a heap
dim t,j
t=r(i)
j=2*i
do while j<=m'j<=m, r(2*i) is the left child of r(i)
if j<m then
if r(j)<r(j+1) then j=j+1'j points to the right child of r(i)
end if
if t<r(j) then'child node has a larger number
r(i)=r(j)'move r(j) to the parent position
i=j'revise the current adjusted node
j=2*i
else
exit do'adjustment completed, exit loop
end if
loop
r(i)=t'the initially adjusted node is placed in the correct position
end sub
'Sort:
sub heapsort(r)
dim i,n,t
n=ubound(r)
for i=fix(n/2) to 0 step -1'build initial heap
sift r,i,n
next
for i=n to 0 step -1'do n+1 passes of sorting
t=r(0)'swap the current heap top number with the last number
r(0)=r(i)
r(i)=t
sift r,0,i-1'rebuild the heap from r(0) to r(i-1)
next
end sub
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
terse +15 2008-04-30 00:02
Floor 10 Posted 2008-04-30 00:04 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Depressed, the single digits cannot have duplicates:
@echo off&setlocal enabledelayedexpansion
set "str=101 20 14 13 19 18 16 11 5"
for %%i in (%str%) do set a=%%i&call :lp
echo %str%
pause>nul&goto :eof
:lp
for %%i in (%str%) do (
if %%i lss %a% (
set str=!str:%a% =!
set str=!str:%%i=%%i %a%!
)
)
批处理之家新域名:www.bathome.net
Floor 11 Posted 2008-04-30 00:11 ·  中国 江苏 常州 武进区 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Is my post on floor 8 not meeting the requirements?
Change `set/p str=输入数字(用空格隔开)` to `set "str=101 20 14 13 19 18 16 11 5"`
It should be okay, right?
简单!简单!再简单!
Floor 12 Posted 2008-04-30 00:18 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by terse at 2008-4-30 00:11:
Is the code on my 8th floor not meeting the requirements?
Change "set/p str=Enter numbers (separated by spaces)" to "set "str=101 20 14 13 19 18 16 11 5"
It should be okay, right?

Terse's code on the 8th floor is really powerful. To be honest, I still don't understand how it works. Brother Terse, can you help explain it?
批处理之家新域名:www.bathome.net
Floor 13 Posted 2008-04-30 00:32 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Originally posted by plp626 at 2008-4-30 12:04 AM:
Why use cmd when you can have the convenience, speed, and efficiency of a Linux shell?
Disclosure: I don't understand Linux.

This doesn't require understanding Linux; you just need to download GNU's sort.exe and call it in a batch script.
Floor 14 Posted 2008-04-30 00:39 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
Originally posted by terse at 2008-4-29 11:56 PM:
@echo off&setlocal enabledelayedexpansion
set/p str=Enter a number (separated by spaces)
for %%i in (%str%) do (
set str=0000000000%%i
set .!str:~-10! !random!=a
)
for /f "delims=.= " ...

Really powerful! I didn't understand it at first...
@echo off&setlocal enabledelayedexpansion
set/p str=Enter a number (separated by spaces)
for %%i in (%str%) do (
set str=0000000000%%i
set .!str:~-10! !random!=a
)
set .
pause
for /f "delims=.= " %%a in ('set .') do for /f "tokens=* delims=0" %%i in ("%%a") do if "%%i"=="" (set/p=0 <nul) else set/p=%%i <nul
pause>nul
After changing the code like this, I got a bit enlightened.
Floor 15 Posted 2008-04-30 01:51 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
I used this before, but I forgot where it came from.
@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:~1%
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
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
plp626 +11 2008-04-30 06:37
huahua0919 +8 2008-04-30 17:18
Forum Jump: