我如何使用批处理文件实现快速排序? [英] How do I implement quicksort using a batch file?

查看:212
本文介绍了我如何使用批处理文件实现快速排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然通常这是很好的选择总是为工作正确的语言,它有时是有益的尝试,并做一些这是不恰当的疯狂的语言。

While normally it's good to always choose the right language for the job, it can sometimes be instructive to try and do something in a language which is wildly inappropriate.


  1. 它可以帮助你理解这个问题更好。也许你不知道的有无的解决它,你以为你做的方式。

  2. 它可以帮助你理解的语言更好。也许它支持更多的功能比你实现。

  1. It can help you understand the problem better. Maybe you don't have to solve it the way you thought you did.
  2. It can help you understand the language better. Maybe it supports more features than you realized.

和推动这一想法,它是不合逻辑的结论......你将如何实现在批处理文件快速排序?它甚至有可能?

And pushing this idea to it's illogical conclusion...how would you implement quicksort in a batch file? Is it even possible?

推荐答案

原来,这不是很难,因为你的想象。语法是丑陋的地狱,但批处理语法实际上是能够一些令人惊讶的事情,包括递归,局部变量和字符串的一些令人惊讶的复杂的解析。不要误会我的意思,这是一个可怕的语言,但出乎我的意料,它不完全瘫痪。我不认为我了解快速排序什么,但我学到了很多关于批处理文件!

Turns out, it's not as hard as you might think. The syntax is ugly as hell, but the batch syntax is actually capable of some surprising things, including recursion, local variables, and some surprisingly sophisticated parsing of strings. Don't get me wrong, it's a terrible language, but to my surprise, it isn't completely crippled. I don't think I learnt anything about quicksort, but I learned a lot about batch files!

在任何情况下,这里的批处理文件快速排序 - 我希望你有尽可能多的乐趣试图了解离奇的语法在阅读它,因为我没有在写它。 : - )

In any case, here's quicksort in a batch file - and I hope you have as much fun trying to understand the bizarre syntax while reading it as I did while writing it. :-)

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

call :qSort %*
for %%i in (%return%) do set results=!results! %%i
echo Sorted result: %results%
ENDLOCAL
goto :eof

:qSort
SETLOCAL
    set list=%*
    set size=0
    set less=
    set greater=
    for %%i in (%*) do set /a size=size+1
    if %size% LEQ 1 ENDLOCAL & set return=%list% & goto :eof
    for /f "tokens=2* delims== " %%i in ('set list') do set p=%%i & set body=%%j
    for %%x in (%body%) do (if %%x LEQ %p% (set less=%%x !less!) else (set greater=%%x !greater!))
    call :qSort %less%
    set sorted=%return%
    call :qSort %greater%
    set sorted=%sorted% %p% %return%
ENDLOCAL & set return=%sorted%
goto :eof

给它一组数字排序的命令行,用空格分隔上调用它。例如:

Call it by giving it a set of numbers to sort on the command line, seperated by spaces. Example:

C:\dev\sorting>qsort.bat 1 3 5 1 12 3 47 3
Sorted result:  1 1 3 3 3 5 12 47

在code是一个有点疼痛的理解。它基本上是快速排序的标准。键位是我们在一个字符串存储号码 - 穷人的数组。在第二个for循环是pretty晦涩,它基本上分裂阵列成头(第一元件)和尾部(所有其它元素)。哈斯克尔做它用符号X:XS,但批处理文件名为与用/ f开关一个循环做到这一点。为什么?为什么不呢?

The code is a bit of a pain to understand. It's basically standard quicksort. Key bits are that we're storing numbers in a string - poor man's array. The second for loop is pretty obscure, it's basically splitting the array into a head (the first element) and a tail (all other elements). Haskell does it with the notation x:xs, but batch files do it with a for loop called with the /f switch. Why? Why not?

在SETLOCAL和ENDLOCAL电话让我们做局部变量 - 样的。 SETLOCAL给我们的原始变量的完整副本,但所有的改变都彻底歼灭当我们调用ENDLOCAL,这意味着你甚至不能使用全局变量调用函数进行通信。这解释了丑ENDLOCAL&放大器;设置回报率=%分类%语法,它的实际工作,尽管什么逻辑将指示。当执行行排序变量还没有被消灭,因为线路尚未执行 - 那么后来因为线路已经执行返回变量不擦拭。逻辑!

The SETLOCAL and ENDLOCAL calls let us do local variables - sort of. SETLOCAL gives us a complete copy of the original variables, but all changes are completely wiped when we call ENDLOCAL, which means you can't even communicate with the calling function using globals. This explains the ugly "ENDLOCAL & set return=%sorted%" syntax, which actually works despite what logic would indicate. When the line is executed the sorted variable hasn't been wiped because the line hasn't been executed yet - then afterwards the return variable isn't wiped because the line has already been executed. Logical!

另外,有趣的是,你基本上不能使用变量里面一个for循环,因为他们无法改变 - 这去除大部分有一个for循环的地步。解决方法是设置ENABLEDELAYEDEXPANSION它的工作原理,但使语法比正常甚至丑陋。请注意,我们现在拥有的只是他们的名字引用的变量组合,由$ P $与单个%pfixing他们,由$ P $两%pfixing他们,通过在包装%他们,或在他们的包裹!而这些不同的引用变量的方式几乎完全不能互换!

Also, amusingly, you basically can't use variables inside a for loop because they can't change - which removes most of the point of having a for loop. The workaround is to set ENABLEDELAYEDEXPANSION which works, but makes the syntax even uglier than normal. Notice we now have a mix of variables referenced just by their name, by prefixing them with a single %, by prefixing them with two %, by wrapping them in %, or by wrapping them in !. And these different ways of referencing variables are almost completely NOT interchangeable!

除此之外,它应该是比较容易理解!

Other than that, it should be relatively easy to understand!

这篇关于我如何使用批处理文件实现快速排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆