Thursday, February 10, 2011

Bubble Sort



%OUT-----------------------------------------------------------------------------------------
%OUT- Arranges an array in ascending order using BUBBLE SORT                     
%out- input: Each term of the array is accepted from the user                         
; Size of the array is mentioned in the program itself
%out- output: Accepted array printed in ASCENDING order                            
%out – author: Ayon and Ariyam                    
%OUT-----------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack
.data
array_size equ 6                              ;assuming array_size to be equal to 6
array db array_size dup(?)                      ;reserving bytes = array_size for the array, uninitialised
ip db "Enter terms of the array: ","$"                
;a request to prompt the user for entering terms of the array
op1 db "Array in ascending order: ","$"                       ;output message
op2 db "Next iteration: ","$"                                            ;intermediate output message            
heading db "***** BUBBLE SORT *****","$"          ;a heading

.code


main proc
            clrscrn                                                                                    ;clearing the screen
            mov dx,0105h
            cursor                                                                                     ;setting up the cursor
            showmsg heading                                                 ;displaying heading
           
            mov dx,0303h                                                         ;pointing to next line
            push dx                                                         ;saving current cursor position
            cursor                                                                         ;setting up the cursor
            input_array array,array_size,ip               ;accept array from user

            mov cx,array_size
            sub cx,01h   
           
            outer:             pop dx                                              ;retrieving the current cursor position
                                    add dh,02h                          ;pointing the cursor to the next line
                                    push dx                                 ;saving current cursor position
                                    cursor
                                    push cx                                             ;saving the current iteration number
                                    mov bx,0001h                     ;loading bx (displacement for based indexed
;addressing with displacement)
                                    mov di,00h               ;pointer to first array element
            inner:              mov al,array[di]    
                                    cmp al,array[di+bx]          ;comparing 2 consecutive array elements
                                    jbe continue
            swap:             mov ah,array[di+bx]         ;performing swap by using two registers ah and al,
;when succeeding array element is smaller
                                    mov array[di],ah
                                    mov array[di+bx],al
            continue:      inc di                          ;point to next array element
                                    loop inner                 ;iterate till the maximum is bubbled down
                                    display_array array,array_size,op2        ;display the result of this iteration
                                    pop cx                                                           ;loader count for outer loop
                                    dec cx
                                    cmp cx,0000h                                             ;short jump not possible
                                    jne outer
                                                                                   
            pop dx
            add dh,02h                                                  ;pointing the cursor to the final line
            cursor
            display_array array,array_size,op1                    ;printing array after sorting

            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.