I ask for your help to write the quicksort algorithm in 8085 assembly. 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? ( I used a simulator)
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
the quicksort in c is:
void quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers
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
the quicksort in c is:
void quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers
;
while (left < right)
{
while ((numbers
while (left < right)
{
while ((numbers
>= pivot) && (left < right))
right--;
if (left != right)
{
numbers
right--;
if (left != right)
{
numbers
= numbers
;
left++;
}
while ((numbers
left++;
}
while ((numbers
<= pivot) && (left < right))
left++;
if (left != right)
{
numbers
left++;
if (left != right)
{
numbers
= numbers
;
right--;
}
}
numbers
right--;
}
}
numbers
= pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}