;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       QSort() (originally "qs()") 
;; 
;;ACTION         Sorts a 1-dimension array 
;; 
;;AUTHOR         BrianTX 
;; 
;;VERSION        1.0 
;; 
;;SYNTAX         qsort(array) 
;; 
;;PARAMETERS     array - Required - Array 
;;               - The array to be sorted 
;; 
;;REMARKS        Sorts numeric or text (ASCII order) arrays 
;; 
;;RETURNS        array, sorted 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    NT4, W2K, WXP 
;;               Explicit, NoVarsInStrings 
;; 
;;EXAMPLES       $Sorted = qsort($Unsorted) 
; 
Function QSort($_a)
 
  Dim $_ls[32],$_us[32],$_sp,$_L,$_U,$_m,$_p,$_i,$_j,$_t
 
  $_ls[0] = 0                                   ; Lower Stack Index 
  $_us[0] = UBOUND($_a)	                        ; Upper Stack Index 
  $_sp    = 0                                   ; Stack Pointer 
 
  While $_sp >=0
    $_l = $_ls[$_sp]
    $_u = $_us[$_sp]
 
    While $_L < $_U
      $_p = $_L + ($_U - $_L) / 2
      $_t = $_a[$_L]
      $_A[$_L] = $_A[$_P]
      $_A[$_P] = $_t
      $_i = $_L + 1
      $_j = $_U
 
:L1
      While ($_i<$_j) And $_A[$_L] > $_A[$_i] 
        $_i = $_i + 1
      Loop
 
      While ($_j>=$_i) And $_A[$_j] > $_A[$_L]
        $_j = $_j - 1
      Loop
 
      IF $_i >= $_j Goto L2 EndIf
      $_t = $_A[$_i]
      $_A[$_i] = $_A[$_j]
      $_A[$_j] = $_t
      $_j = $_j - 1
      $_i = $_i + 1
      Goto L1
 
:L2
      $_t = $_a[$_l]
      $_a[$_l] = $_a[$_j]
      $_a[$_j] = $_t
      $_m = $_j
 
      If $_m - $_l <= $_u - $_m
        If $_m + 1 < $_u
         $_ls[$_sp] = $_m + 1
         $_us[$_sp] = $_u
         $_sp = $_sp + 1
        Endif
        $_u = $_m - 1
      Else
        If $_m - 1 > $_l
          $_ls[$_sp] = $_l
          $_us[$_sp] = $_m - 1
          $_sp = $_sp + 1
        Endif
        $_l = $_m + 1
      Endif
    Loop
    $_sp = $_sp - 1
  Loop
 
  $QSort = $_a
 
Endfunction