Thursday, February 10, 2011

Selection Sort


%OUT--------------------------------------------------------------------------
%OUT- Performs Selection Sort on an array stored accepted from user
%out- output: Display the sorted array
%out – author: Ayon and Ariyam
%OUT--------------------------------------------------------------------------

if1
include macrolib.lby ;contains common macros
endif

.model small
.stack
.data
array db 11 dup(?) ;array length to be specified in decimal
count dw 1 dup(?) ;array length
msg db " Enter Array Elements: ","$"
i_msg db " Enter Array length ","$"
o_p db "Sorted array: ","$"
i_p db "Original array: ","$"
heading db "---< SELECTION SORT >--- ","$"
next db " Next iteration: ","$"

.code

main proc

mov ax,@data
mov ds,ax
showmsg heading
showmsg i_msg
input
lea bx,count
mov [bx],al
input_array array,count,msg
display_array array,count,i_p ;display the original array
;prepare for sorting
mov cx,count
dec cx
mov di,0000h
outer:
mov si,di
inc si
mov ah,array[di] ;points to present index position to decide
inner:
mov al,array[si] ;all positions after the present position to decide
cmp al,ah
jb update
back:
inc si
cmp si,count
jne inner
inc di
display_array array,count,next
loop outer
jmp done
update:
mov array[si],ah
mov array[di],al
mov ah,al
jmp back
;display the sorted array
done:
display_array array,count,o_p
exit

main endp
end main

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.