Thursday, February 10, 2011

Insertion Sort


%OUT--------------------------------------------------------------------------------
%OUT- Performs Insertion Sort on an array accepted from the 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
lf equ 10d ;line feed
tab equ 09d ;print tab
array db 11 dup(?) ;array to be sorted
count dw 1 dup(?) ;array length
msg db lf,"Enter Array Elements ","$"
i_msg db lf,"How many elements? ","$"
o_p db lf,"Sorted array: ","$"
i_p db lf,"Original array: ","$"
sort db lf,"---< INSERTION SORT >---","$"
next db lf,"Next Iteration: ","$"

.code
main proc

mov ax,@data
mov ds,ax
showmsg i_msg
input
lea di,count
mov [di],al
input_array array,count,msg
showmsg i_p
mov cx,count
mov si,0000h
lp1:
mov bh,array[si]
output
space
inc si
loop lp1
;prepare for sorting
mov ax,@data
mov ds,ax
;mov bx,0001h
mov cx,count
mov si,01h ;i=>si

for_loop:
mov al,array[si]
mov di,si
dec di ;j=>di
while_loop:
cmp di,0ffffh ;while(j>=0 && arr[j]>item)
je outofwhile
cmp array[di],al
jb outofwhile
mov ah,array[di] ;arr[j+1]=arr[j]
mov array[di+01h],ah
dec di
jmp while_loop
outofwhile:
inc di
mov array[di],al ;arr[j+1]=item;
inc si
display_array array,count,next
dec cx
cmp cx,0001h
jne for_loop

display_array array,count,o_p ;display sorted array
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.