Fast Quick sort

[Autolink] Menu

This is a fast quick sort routine.

Author: Ruediger Wilke

 
/* ------------------------------------------------------------------ */
/* function: quick sort routine                                       */
/*                                                                    */
/* call:     QuickSort first_element, last_element                    */
/*                                                                    */
/* returns:  nothing                                                  */
/*                                                                    */
/* notes:    You must save the elements to sort in the stem "a."      */
/*           a.0 must contain the number of elements in the stem.     */
/*                                                                    */
/*                                                                    */
qqsort: procedure expose a.

  arg lf, re

  if re -lf < 9 then
    do lf = lf to re -1

      m = lf

      do j = lf +1 to re
        if a.j << a.m then                                   /* v2.80 */
          m = j
      end /* j = lf +1 to re */

      t = a.m; a.m = a.lf; a.lf = t

    end /* lf = lf to re -1 */
    else
    do
      i = lf
      j = re
      k = (lf + re)%2
      t = a.k

      do until i > j

        do while a.i << t                                    /* v2.80 */
          i = i + 1
        end /* while a.i << t */

        do while a.j >> t                                    /* v2.80 */
          j = j - 1
        end /* while a.j >> t */

        if i <= j then
        do
          xchg = a.i
          a.i = a.j
          a.j = xchg
          i = i + 1
          j = j - 1
        end /* if i <= j then */

      end /* until i > j */

      call qqsort lf, j
      call qqsort i, re
    end /* else */

return


[Back: Bubble Sort]
[Next: Flexible Quick sort]