Over the years I've seen a lot of discussion (and several implementations) of the Quicksort algorithm. Most of what I have seen, unfortunately, lacks sufficient commenting as well as meaningful variable names. Hopefully the following vbScript code will more clearly show how Quicksort actually works.
A couple of incidental notes:
- I use PrimalScript for editing and I have comments set to display with a silver-grey background and black text. This makes comments very distinct from code (and is also why I have a closing single quote at the end of lines).
-
The test code section at the end executes only if I run the QuickSort.vbs file directly (a trick I learned from Python).
I keep all my common code in an include folder
D:\Include
with an envorment variable, %INCLUDE% set to that location. That way, the only code I have to duplicate isFunction Include ( ByVal file )
Dim wso: Set wso = CreateObject("Wscript.Shell")
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
ExecuteGlobal(fso.OpenTextFile(wso.ExpandEnvironmentStrings(file)).ReadAll)
End Function
in any script that needs library code. I can do
Include "%INCLUDE%\QuickSort.vbs"
<edit>The sample code was edited to correct a bug, not in the actual quicksort algorithm, but in the comparison test passed to quicksort. The original test was "X1 <= X2"
and it has been corrected to "X1 < X2"
.