home *** CD-ROM | disk | FTP | other *** search
- ***********************************************************
- * Quick Sort
- ***********************************************************
- * Sorts a random array of integers in N*log(N) time.
- *
- * Input:
- * 4(sp) : ptr to first element
- * 8(sp) : ptr to last element
- *
- * Output:
- * none
- *
- * Uses:
- * d0 : "partition" element.
- * d1 : temporary used to exchange elements.
- * a0 : first element in array.
- * a1 : last element in array.
- * a2 : scanning pointer
- * a3 : scanning pointer
- ***********************************************************
-
- first EQU 4
- last EQU 8
-
- ExgM MACRO \1,\2
- move.l \1,d1 ; 12 cycles
- move.l \2,\1 ; 20 cycles
- move.l d1,\2 ; 12 cycles
- ENDM
-
- ***********************************************************
-
- QuickSort: move.l first(sp),a0 ; first element
- move.l last(sp),a1 ; last element
-
- .QSort: cmp.l a1,a0 ; are we done yet?
- bgt.b .done ; yes. begin recursive return.
-
- move.l (a1),d0 ; No. Load the partition variable,
- move.l a0,a2 ; the left side incrementing ptr,
- move.l a1,a3 ; and the right side dec. ptr.
-
- .loop1: cmp.l (a2)+,d0 ; <---+ Scan left ptr up for a swap
- bgt.b .loop1 ; >---+
- subq.l #4,a2 ; | Get rid of last post inc.
- .loop2: cmp.l -(a3),d0 ; <-+ | scan right ptr dwn for a swap
- blt.b .loop2 ; >-+ |
- cmp.l a3,a2 ; | Did the pointers cross?
- bgt.b .break ; >-+ | Yes, break out of the loop
- ExgM (a2),(a3) ; | | Swap the out of place ele.
- bra.b .loop1 ; >-|-+
- ; |
- .break: ExgM (a1),(a2) ; <-+ Swap them.
- move.l a1,-(sp) ; last element for 2nd recur.
- pea 4(a2) ; first element for 2nd recur.
- lea -4(a2),a1 ; last element for 1st recur.
- bsr.b .QSort ; first recur.
- bsr.b QuickSort ; second recur.
- addq.l #8,sp ; restore stack
- .done: rts ; return to caller.
-