Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 46261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:56:18+00:00 2026-05-10T15:56:18+00:00

While normally it’s good to always choose the right language for the job, it

  • 0

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. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:56:18+00:00Added an answer on May 10, 2026 at 3:56 pm

    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 

    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?

    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!

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I can normally do commits and also push to other remote repositories (eg.
Normally I make something like this. #1 first query while () : #2 2nd
I know that I can't modify a collection while iterating over it. Normally I
Normally when you iterate through a mysql result you do something like this: while
Ruby, which is normally very forgiving, is giving me an error on a while
While normally I use the Subversive plugin (Eclipse) for SVN, I've decided to try
VMWare Workstation does something quite cool when I try to shutdown Windows while a
While attempting to debug a job submission script, I ended up narrowing down the
While refactoring my code base I found a piece of code which I'd like
Normally, when I want to cancel a backgroundWorker in C# I will do something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.