My homework is to write the quicksort algorithm in 8085 assembly. So I tried to write it and of course it has too many bugs but my main problem is that I don't know how to handle the recursive character of the algorithm and control the loops. For example, what kind of flag should i use to know if the 1st partition is sorted so to call quicksort for the second partition?
The dataset I used is 37 2 6 4 89 8 10 12 68 45 starting from memory position 3000H and regs bc: 1st element index/ de: last elem index
If u or anyone else could take a look, I would be thankful.
Here is my code:
;initialisation
LXI SP,FFF0H
LXI H,3009H
XCHG
MVI B,30H
MVI C,00H
QSORT:
MOV H,B
MOV L,C
MOV A,M
MOV L,E
SCANR:
CMP M
PUSH PSW
CNC SWAP
CPI 01H
JZ SCANL
DCX H
JMP SCANR
SCANL:
POP PSW
MOV C,L
LXI H,3000H
LOOP:
INX H
CMP M
PUSH PSW
PUSH B ;;;
CC SWAP
JZ RECURSIVE
CPI 01H
JNZ LOOP
POP H ;;;
POP PSW
JMP SCANR
SWAP:
PUSH B
PUSH D
PUSH H
MOV D,M
MOV L,C
MOV E,M
MOV M,D
POP H
MOV M,E
MVI A,01H
POP D
POP B
MOV C,L
RECURSIVE:
;BREAK
LXI B,3000H
DCX H
MOV E,L
JMP QSORT ; I<X
QS2:
LXI B,3009H
;BREAK
INX H
INX H
MOV E,L
JMP QSORT
HLT
:?: