After reading various tutorials and Stackoverflow answers for hours on this topic, I tried to implement this algorithm in emu8086 in my own way; although I am very close to completing this but after fixing many errors, i am unable to understand where I went wrong, I would appreciate if anyone would help me with this.
select_sort PROC
pusha ; push all register values
mov bp, sp
mov cx, [bp+20] ; transfer no. of integers to cx
mov bx, [bp+18] ; and integer array address to bx
loop_outer:
dec cx
jz done ; if cx=0, done
mov di, cx ; else another iteration
mov dx, SORTED ; assume status of array as sorted
mov si, bx ; transfer array address to si
loop_inner:
mov al, [si]
cmp al, [si+2]
jc resume
mov ah, [si+2]
resume:
add si, 2
dec di
jnz loop_inner
cmp dx, SORTED
je done
jmp swap
swap:
xchg ax, [si+2] ; swap integers at [si] and [si+2]
mov [si], ax
mov dx, UNSORTED ; mark status of array as unsorted
jmp loop_outer
done:
popa ; pop to respective register locations
ret 4
select_sort ENDP
My input : 534210
Output : 53421
Expected output : 12345
Although I think the logic of my selection sort algorithm is okay, I am not able understand why I am not getting the exact output.