Board logo

标题: ********[原创]直接插入排序******** [打印本页]

作者: s11ss     时间: 2007-9-14 22:35    标题: ********[原创]直接插入排序********

@echo off
setlocal enabledelayedexpansion

:::::::::::::::::::::::::::::::::::::::::Straight Insertion Sort:::::::::::::::::::::::::::::::::::::::::


::::::::Get Original Numbers::::::::
echo Please input some numbers separated from "," , like 2007,9,14,88:
set /a s=0
set /p str=
set /a len=0
set /a nx=0
:c
call set word=%%str:~%len%,1%%
set /a len+=1
if "%word%" equ "," (set /a nx+=1 & call :e)
if not "%word%" equ "" (goto :c)
set /a nx+=1
call :e
goto :o
:e
set /a sn=%len%-%s%-1
call set n%nx%=%%str:~%s%,%sn%%%
set /a s=%len%
goto :eof
::::::::Get Original Numbers::::::::

::::::::Deal With Numbers::::::::
:o
set /a num=%nx%+1
set /a i=2
:m
set /a temporary=!n%i%!
set /a j=%i%-1
:j
if %temporary% lss !n%j%! (goto :ch)
goto :z
:ch
set /a k=%j%+1
set /a n%k%=!n%j%!
set /a j-=1
if %j% gtr 0 goto :j
:z
set /a k=%j%+1
set /a n%k%=%temporary%
set /a i+=1
if %i% lss %num% goto :m
::::::::Deal With Numbers::::::::

::::::::Print The Result::::::::
for /l %%a in (1,1,%nx%) do (set str2=!str2!!n%%a! )
echo.
echo The numbers after sorting are:
echo !str2!
echo.
echo Press Any Key To Exit...
pause>nul
goto :eof
::::::::Print The Result::::::::


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::s11ss
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::2007-9-14





















***************************
说明:
原先的goto :j这一句导致对某些存在负数的情况处理不够好,改成
if %j% gtr 0 goto :j以后就解决了.

[ Last edited by s11ss on 2007-9-16 at 10:43 AM ]
作者: yanjingtu     时间: 2007-9-15 01:10
占个位子。
赞一个!!!