Category : Files from Magazines
Archive   : VOL7N16.ZIP
Filename : QSORTI.ASM

 
Output of file : QSORTI.ASM contained in archive : VOL7N16.ZIP
name qsorti
title QSORTI.ASM Integer QuickSort
page 55,132
;
; QSORTI.ASM --- Integer Quicksort
;
; Copyright 1988, Ziff Communications Co.
; PC Magazine * Ray Duncan
;
; Call with: DS:SI = address of first item to sort
; DS:DI = address of last item to sort
; Assumes items are 2-byte integers
;
; Returns: Nothing, all registers unchanged.
;

itemsiz equ 2 ; bytes per integer

_TEXT segment word public 'CODE'

assume cs:_TEXT,ds:NOTHING,es:NOTHING

; stack variables
left equ [bp-8] ; first item to sort
right equ [bp-4] ; last item to sort

public qsorti ; make visible to Linker

qsorti proc near

cmp di,si ; if right <= left
jna qsort5 ; just exit

push bp ; set up stack frame
mov bp,sp ; and local variables
push es
push di ; offset last item
push ds
push si ; offset first item
push dx
push cx
push bx
push ax

sub si,itemsiz ; SI = i = left - 1
; DI = j = right
mov bx,di ; BX = right

qsort1: ; partition array on
; value of rightmost item

qsort2: ; scan right for item
; >= partitioning value

add si,itemsiz ; i++

mov ax,[si] ; while(items[i] < items[right])
cmp ax,[bx] ; (guaranteed to terminate
jl qsort2 ; when i = right)

qsort3: ; scan left for item
; <= partitioning value

sub di,itemsiz ; j--

cmp di,left ; while(items[j] > items[right]
jna qsort4 ; && (j > left)
mov ax,[di]
cmp ax,[bx]
jg qsort3

qsort4: mov ax,[di] ; exchange the items
xchg ax,[si]
mov [di],ax

cmp di,si ; while(j > i)
ja qsort1 ; (do until pointers cross)

mov ax,[di] ; undo the last exchange
xchg ax,[si]
mov [di],ax

mov ax,[bx] ; put the partitioning
xchg ax,[si] ; element into position
mov [bx],ax

push si ; save i

mov di,si ; qsort(left, i-1)
sub di,itemsiz
mov si,left
call qsorti

pop si ; qsort(i+1, right)
add si,itemsiz
mov di,right
call qsorti

pop ax ; restore registers,
pop bx ; discard stack frame
pop cx
pop dx
pop si
pop ds
pop di
pop es
pop bp

qsort5: ret ; return to caller

qsorti endp

_TEXT ends

end


  3 Responses to “Category : Files from Magazines
Archive   : VOL7N16.ZIP
Filename : QSORTI.ASM

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/