Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bubble Sort

94 views
Skip to first unread message

Mike Sanders

unread,
May 18, 2017, 2:10:00 PM5/18/17
to
Nifty little string sorting routine without
the need of calling an external utility.


@echo off & setlocal enabledelayedexpansion

:: Bubble Sort for batch/cmd: Michael Sanders - 2017
:: https://busybox.hypermart.net/bubble-sort.html
::
:: usage: bsort cb321a
:: output: 123abc
::
:: default is sort acending [a-z]
:: to sort descending [z-a]...
::
:: change: if defined b if !a! gtr !b!
:: to: if defined b if !a! lss !b!
::
:: collating order...
::
:: 32 SPC 37 % 42 * 47 / 52 4
:: 33 ! 38 & 43 + 48 0 53 5
:: 34 " 39 ' 44 , 49 1 54 6
:: 35 # 40 ( 45 - 50 2 55 7
:: 36 $ 41 ) 46 . 51 3 56 8
::
:: 57 9 62 > 67 C 72 H 77 M
:: 58 : 63 ? 68 D 73 I 78 N
:: 59 ; 64 @ 69 E 74 J 79 O
:: 60 < 65 A 70 F 75 K 80 P
:: 61 = 66 B 71 G 76 L 81 Q
::
:: 82 R 87 W 92 \ 97 a 102 f
:: 83 S 88 X 93 ] 98 b 103 g
:: 84 T 89 Y 94 ^ 99 c 104 h
:: 85 U 90 Z 95 _ 100 d 105 i
:: 86 V 91 [ 96 ` 101 e 106 j
::
:: 107 k 112 p 117 u 122 z 127 DEL
:: 108 l 113 q 118 v 123 {
:: 109 m 114 r 119 w 124 |
:: 110 n 115 s 120 x 125 }
:: 111 o 116 t 121 y 126 ~

:setup

set unsorted=%*
set buf=%unsorted%
set len=0

:getlength

if not defined buf goto :split
set buf=%buf:~1%
set /a len+=1
goto :getlength

:split

if %len% leq 0 (echo bubble sort: zero length string & exit /b 1)
set /a len-=1
for /l %%x in (0,1,!len!) do set u[%%x]=!unsorted:~%%x,1!

:bubblesort

set c=0
for /l %%x in (0,1,%len%) do (
set /a p=%%x+1
call set a=!u[%%x]!
call set b=%%u[!p!]%%
if defined b if !a! gtr !b! (
set u[!p!]=!a!
set u[%%x]=!b!
set c=1
)
)
if !c!==1 goto :bubblesort

:done

for /l %%x in (0,1,!len!) do set sorted=!sorted!!u[%%x]!
echo. & echo unsorted input: !unsorted!
echo. & echo sorted output: !sorted!
endlocal & exit /b 0

:eof


--
later on,
Mike

https://busybox.hypermart.net

mokomoji

unread,
May 19, 2017, 12:19:25 PM5/19/17
to
2017년 5월 19일 금요일 오전 3시 10분 0초 UTC+9, Mike Sanders 님의 말:
How do you use it?

When you run it, the window goes out.

@echo off
call filename.cmd cb321a
pause
??????????????????????
no output??????????????


arry with uniq char...sort

@echo off
set /p num=cb321a?
:l
if "%num%" equ "" goto :end

set num2=%num:~0,1%
set num3[%num2%]=%num:~0,1%
set num=%num:~1%
goto :l

:end
set num3

for /f "usebackq tokens=2 delims==" %%f in (`set num3`) do (
call set num4=%%num4%%%%f
)
echo %num4%
pause

input cb312a
output 123abc

-,.-'


arry with sort line all char


@echo off
setlocal
set /p z_num=cb321a?
:l
if "%z_num%" equ "" goto :end
set /a z_num2+=1
set z_num4=000000%z_num2%
set z_num4=%z_num4:~-5%
set z_num3[%z_num:~0,1%_%z_num4%]=%z_num:~0,1%
set z_num=%z_num:~1%
goto :l

:end
set z_num3


for /f "usebackq tokens=2 delims==" %%f in (`set z_num3`) do (
call set z_num5=%%z_num5%%%%f
)
echo %z_num5%

endlocal
pause



"Bubble sort" seems to be hard

setlocal enabledelayedexpansion <-- bad snytex..

use xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx cause bug snytex

Mike Sanders

unread,
May 19, 2017, 3:05:59 PM5/19/17
to
mokomoji <moko...@naver.com> wrote:

> "Bubble sort" seems to be hard

Its very simple...

if the left character is greater than the
adjacent right character then swap positions.
Consider...

set s=BA

Since character B == 66 in ASCII decimal
& character A == 65, this means B > A,
so swap their positions:

set s=AB

mokomoji

unread,
May 19, 2017, 9:54:00 PM5/19/17
to
2017년 5월 19일 금요일 오전 3시 10분 0초 UTC+9, Mike Sanders 님의 말:
???????????
you big trouble

you big trouble

you big trouble

you big trouble

you big trouble


How do you use it?

When you run it, the window goes out.


frist big trouble

You released the source and released a source that no one could use.

Write manual


mokomoji

unread,
May 19, 2017, 10:03:07 PM5/19/17
to

why made you do it?
why disclose do it?
i'm Your source is not available

Execute you'r batch file
cmd windows close

내 컴퓨터에서 니 파일 작동 안한다고..
무슨 알고리즘 타령이야..
만들면 뭐하냐고? 쓸 수가 없는 걸..

메뉴얼을 쓰던가.. 아니면 결과값 나오게 만들던가..

사용법 써 놓으라공...ㅇㅁㅇ

Mike Sanders

unread,
May 20, 2017, 11:28:51 AM5/20/17
to
A more streamlined version (less string handling)
& configurable sorting (set seq)...

@echo off & setlocal enabledelayedexpansion

:: Bubble Sort for batch/cmd v1.01 Michael Sanders
:: https://busybox.hypermart.net/bubble-sort.html
::
:: usage: bsort cb321a
:: output: 123abc
::
:: to sort ascending [a-z]: set seq=gtr
:: to sort descending [z-a]: set seq=lss
::
:: collating order...
::
:: 32 SPC 51 3 70 F 89 Y 108 l
:: 33 ! 52 4 71 G 90 Z 109 m
:: 34 " 53 5 72 H 91 [ 110 n
:: 35 # 54 6 73 I 92 \ 111 o
:: 36 $ 55 7 74 J 93 ] 112 p
:: 37 % 56 8 75 K 94 ^ 113 q
:: 38 & 57 9 76 L 95 _ 114 r
:: 39 ' 58 : 77 M 96 ` 115 s
:: 40 ( 59 ; 78 N 97 a 116 t
:: 41 ) 60 < 79 O 98 b 117 u
:: 42 * 61 = 80 P 99 c 118 v
:: 43 + 62 > 81 Q 100 d 119 w
:: 44 , 63 ? 82 R 101 e 120 x
:: 45 - 64 @ 83 S 102 f 121 y
:: 46 . 65 A 84 T 103 g 122 z
:: 47 / 66 B 85 U 104 h 123 {
:: 48 0 67 C 86 V 105 i 124 |
:: 49 1 68 D 87 W 106 j 125 }
:: 50 2 69 E 88 X 107 k 126 ~

:setup

set seq=gtr
set str=%*
set buf=%*
set len=0

:getlength

if not defined buf goto :chklength
set buf=%buf:~1%
set /a len+=1
goto :getlength

:chklength

if %len% leq 0 (echo bubble sort: zero length string & exit /b 1)
set /a len-=1

:bubblesort

set /a c=0
for /l %%x in (0,1,%len%) do (
set /a y=%%x+1
call set a=!str:~%%x,1!
call set b=%%str:~!y!,1%%
if defined b if !a! %seq% !b! (
call set str=%%str:!a!!b!=!b!!a!%%
set /a c=1
)
)
if %c% equ 1 goto :bubblesort

:done

echo unsorted input: %*
echo sorted output: %str%
endlocal & exit /b 0



Mike Sanders

unread,
May 20, 2017, 11:40:33 AM5/20/17
to
mokomoji <moko...@naver.com> wrote:

First, see the newer streamlined version upthread...

> How do you use it?

bsort 321

bsort cba

It only sorts printable ASCII characters.

> When you run it, the window goes out.

Well open a command prompt & 'cd' to the folder
containing the script, c'mon now...

> setlocal enabledelayedexpansion <-- bad snytex..

It looks like you're cmd.exe doesn't understand
'enabledelayedexpansion' I cant change that.

> Write manual

Again no, you're not reading closely enough.

See the script's home page & study the notes section
for documentation about bubble sort:

<https://busybox.hypermart.net/bubble-sort.html>

> You released the source and released a source that no one could use.

Non-sense. The script works fine (Win7 & Win10),
you're either trolling or not paying attention.
If you're trolling, consider this your only warning chief.
I'll mark your posts as ignored in a new york minute, so
please behave & we can figure out what problems you're
having & together try to solve it.

mokomoji

unread,
May 20, 2017, 6:11:16 PM5/20/17
to
0. windows 10 kor version
1. user-desktop - new folder make
2. open new folder
3. open notepad
4. write use'r cmd source
5. new-name "bsort.cmd" ascii code write
6. cmd open windows shell
7. change directory user-desktop "new folder" move
8. english change mode chcp 437 <-- eng cmd mode
9. new folder\bsort.cmd 321acb
10 .................black screen 10 min wait.......
11 .................black screen 20 min wait.......
12 ctrn+c
13 ctrn+c
14 ctrn+c
15 ctrn+c
16 ctrn+c

?

bsort 321
bsort cba
it's same...

It only sorts printable ASCII characters.

no out-put

black screen

screen...stop



Well open a command prompt & 'cd' to the folder
containing the script, c'mon now...

ㅇㅁㅇ????? open command prompt - cd ok..open folder
execute file...
error

you'r source Non-sense.
use you'r source? down menual?

open cmd?
cd path?
execute file?
else output print...??

output.. no print..

It is your wrong consciousness.

why?

When running cmd on Windows, the output value should be stopped and shown.

If you violate this principle?

------------------------------
@echo off
:d
goto :d
------------------------------

What is the difference between your source and this source?
you source
It's the same for me

bsort value???

it's what bsort??????
filename?
function?
labal???

There is nothing definite in your source and manual

i'am source

set /p z_num=cb321a? ::input line
echo %z_num5% :: output line
pause :: display pause line




modul source execute sample

call filename.cmd %1 %2 %3 <--- sample ok?


execute you'r source..
-------------------------------
@echo off
call bsort.cmd "cba312"
pause

-------------------------------

It is consideration for those who verify the source.

but no execute you'r source...

Mike Sanders

unread,
May 21, 2017, 2:51:43 PM5/21/17
to
mokomoji <moko...@naver.com> wrote:

> 0. windows 10 kor version

Hello moko. Ahh. I see now. Well, I can't reproduce the problem
you're having & I don't have access to a Korean version
of Windows. So, I don't know how to help you with this problem...

> you'r source Non-sense.
> use you'r source? down menual?

I'm sorry moko. The script runs fine & okay here & other
computers I've tried it on. The problem is on your end.
If I could help you, I would. =)

> When running cmd on Windows, the output value should
> be stopped and shown.

Then place a pause at the script's end if you feel it will help.
I trust readers will know this.

> ------------------------------
> @echo off
> :d
> goto :d
> ------------------------------
>
> What is the difference between your source and this source?
> you source
> It's the same for me

I understand your example (just a empty loop). Sorry to read this,
but again it works fine here, it really it does.

> There is nothing definite in your source and manual.

Maybe you can build a bubble sort that will works with
Asian versions of Windows & post it here?

> english change mode chcp 437 <-- eng cmd mode

Windows Vietnamese:
chcp 1258
bsort cba
output abc

Windows Thai:
chcp 874
bsort cba
output abc

Windows Arabic:
chcp 1256
bsort cba
output abc

UTF8:
chcp 65001
bsort cba
output abc

cmd /u:
bsort cba
output abc

cmd /a:
bsort cba
output abc

Windows Koren?
chcp 1363
Invalid code page

Windows Koren?
chcp 949
Invalid code page

My info comes from:

<https://en.wikipedia.org/wiki/Windows_code_page#OEM_code_page>

But I want to say thank you for letting me know of the
troubles you're having. If you develop I fix for the issue,
please feel free to show me.

Ammammata

unread,
May 22, 2017, 3:09:26 AM5/22/17
to
Il giorno Thu 18 May 2017 08:06:37p, *Mike Sanders* ha inviato su
alt.msdos.batch.nt il messaggio news:ofknrc$301$1...@dont-email.me. Vediamo
cosa ha scritto:

> Nifty little string sorting routine without
> the need of calling an external utility.
>

it works! :)

but please check for exclamation mark: you wrote "It only sorts printable
ASCII characters"

c:\Tools>bsort itworks!
unsorted input: itworks
sorted output: ikorstw

why did it drop the '!' ?

--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
>>>>> http://www.bb2002.it :) <<<<<
........... [ al lavoro ] ...........

Mike Sanders

unread,
May 22, 2017, 3:27:35 AM5/22/17
to
Ammammata <amma...@tiscalinet.it> wrote:

Hi Ammammata.

> why did it drop the '!' ?

Because '!' like '>' or '|' (& others) are shell *meta* characters,
this means these types of characters must first be escaped before
feeding them to the script. This is true of every scripting langauge
under every OS. For Windows see:

<http://www.robvanderwoude.com/escapechars.php>

For Unix, its more complex...

Mike Sanders

unread,
May 22, 2017, 7:00:55 AM5/22/17
to
Mike Sanders <mi...@porkchop.bsd> wrote:

> shell *meta* characters...

I've updated that page:

<https://busybox.hypermart.net/bubble-sort.html>

so it explitcly notes meta characters:

! & ^ | < = >

For folks who may not know or have forgotten.

mokomoji

unread,
May 22, 2017, 11:19:23 AM5/22/17
to
2017년 5월 19일 금요일 오전 3시 10분 0초 UTC+9, Mike Sanders 님의 말:
u source error line find...


:getlength
if not defined buf goto :chklength
set buf=%buf:~1%
set /a len+=1
goto :getlength


if not defined buf goto :chklength
if not defined buf goto :chklength
if not defined buf goto :chklength
if not defined buf goto :chklength

set buf <-- no demolition

it's variable name no demolition

not defined
variable demolition syntex
set buf=


u'r source insert
:getlength
if not defined buf goto :chklength

set buf
pause

set buf=%buf:~1%
set /a len+=1
goto :getlength


live buf variable
unlimited loof
u'r buf value is nul
if "%buf%" equ "" goto :chklength

and
hiden value

set buf=%buf:~1% <-- speace

editing

set "buf=%buf:~1%"



user' source...


@echo off
setlocal enabledelayedexpansion
:setup
set "seq=gtr"
set "str=%*"
set "buf=%*"
set "len=0"
:getlength
if "%buf%" equ "" goto :chklength
set "buf=%buf:~1%"
set /a "len+=1"
goto :getlength

:chklength
if %len% leq 0 (
echo "bubble sort: zero length string"
exit /b 1
)
set /a "len-=1"
:bubblesort
set /a "c=0"
for /l %%x in (0,1,%len%) do (
set /a "y=%%x+1"
call set "a=!str:~%%x,1!"
call set "b=%%str:~!y!,1%%"
if defined b if !a! %seq% !b! (
call set "str=%%str:!a!!b!=!b!!a!%%"
set /a "c=1"
)
)
if %c% equ 1 goto :bubblesort

:done

echo unsorted input: %*
echo sorted output: %str%
endlocal & exit /b 0

C:\Users\mokom\Desktop\새 폴더>12374.cmd avd4321
unsorted input: avd4321
sorted output: 1234adv



Mike Sanders

unread,
May 22, 2017, 11:54:04 AM5/22/17
to
mokomoji <moko...@naver.com> wrote:

Moko, question...

Did you copy the script from inside your newsreader
or did you download the script from its webpage?

The reason I ask is because you wrote:

> u'r buf value is nul

And then

> set buf=%buf:~1% <-- speace

It cant be both nul & have a space too
& the script I wrote had neither values...

This makes me wonder if your newsreader (your post headers
indicate you're using of google groups) is displaying the
script as HTML? (With future scripts please down from site,
script will be inside zip file to protect it)

> unsorted input: avd4321
> sorted output: 1234adv

Ahh, good man =) You got it working on your end.

Mike Sanders

unread,
May 22, 2017, 12:02:20 PM5/22/17
to
Mike Sanders <mi...@porkchop.bsd> wrote:

> (With future scripts please down from site,
> script will be inside zip file to protect it)

Mike's Scripting Site: Index / Download / Log

-------------------------------^^^^^^^^

mokomoji

unread,
May 23, 2017, 9:12:19 AM5/23/17
to
2017년 5월 19일 금요일 오전 3시 10분 0초 UTC+9, Mike Sanders 님의 말:
u'r site? zip download????

So when I put the source up and write down my site address
Do you visit?

It is basically copied from Google Newsgroup.

http://blog.naver.com/mokomoji/130174186424
my site souce..
my site file down load..

Do you understand my source?

no english?
no understand ?
yes but

There is also a translate button at the top of the web page.

You tell a very dangerous story.
don't Visit sites, you do not know about malware or ransomware,
You should not download the file.
You are guiding it.

Mike Sanders

unread,
May 28, 2017, 5:52:48 PM5/28/17
to
mokomoji <moko...@naver.com> wrote:

> So when I put the source up and write down my site address
> Do you visit?

Yes =) very nice.

> Do you understand my source?

Most of it.

> There is also a translate button at the top of the web page.

Aye.

> You tell a very dangerous story.

Well moco, please allow me to say this... I'm not going to live fear,
or viri (I'll simply reformat & reinstall the ISO).

Also, just to clear the air between us all here in the group...
I'm not worried about being a rock-start programmer. I'm simply
going to offer what I've learned for others to learn from as well
in the hope that if I share my knowledge, others too will share
with me. I seek to learn & that's all. =)

Mike Sanders

unread,
May 28, 2017, 6:02:14 PM5/28/17
to
Mike Sanders <mi...@porkchop.bsd> wrote:

> I'm not worried about being a rock-start programmer.

Rock start? chuckle, make that rock star instead.

My posts are hardly ever 'error-three'!

Doh wait I meant - error-free...
0 new messages