回 帖 发 新 帖 刷新版面

主题:[讨论]求助:冒泡排序程序(十进制数输入部分实现)

我是一个计算机专业的在校学生,老师布置给我一道题目.大致内容是:
从键盘输入十个十进制数(1--2位),然后排序存储,最后输出(升序).

我已经把排序存储和输出部分写好了,但输入部分不知道如何下手,
请大哥大姐们帮下忙,给写下下...3Q

回复列表 (共4个回复)

沙发

我觉得可以规定一下格式,就规定为2位好了,单位数如1用01表示,这样子长度固定好处理一些……冒昧的问一下,你是哪个学校的啊?

板凳

.model small                                    ;对一个已知数组元素大小排序                                ;

.stack

.data
      array  db   1h,0ah,3h,8h,5h,6h,7h,4h,9h,2h 
  
.code

start:                                          ;10 
      mov ax,@data
      mov ds,ax
      mov si,0
      mov di,1
one:
      mov bl,array[si]                           ;通过改变si,di的数值,使元素互换,达到排序的功能
      mov bh,array[di]
      cmp bh,bl                                   
      jb  two                
      inc di
      cmp di,10
      jz  three
      jmp one

two:                                             ;互换元素    25
      mov al,array[si]
      mov ah,array[di]
      mov array[di],al
      mov array[si],ah
      inc di
      cmp di,10
      jz three
      jmp one

three:                                            ;通过判断si的大小,决定循环是否结束
      inc si
      cmp si,9
      jz  four
      mov bx,si
      mov di,bx            ;40
      inc di 
      jmp one

four:
      mov si,0

lable1:                                          ;显示作用 
      cmp si,9
      ja  six 
      add array[si],30h
      cmp array[si],39h
      jbe five
      add array[si],7

five: 
      mov dl,array[si]
      mov ah,02h
      int 21h
      mov dl,0ah
      mov ah,02h
      int 21h
      inc si
      jmp lable1   

six:
.exit
      end start          

这是我前一段写的,不过是对固定得数组排序
你可以修改一下

3 楼

完全不用这么长的代码……

4 楼

data segment
    buf db 4,5,2,3,4,5,6,7,8,9   ;高位高地址,低位低地址
    num_cx dw ?
    num_cx_1 dw ?
data ends

code segment
    assume cs:code,ds:data
start:
    mov ax,data
    mov ds,ax  
    
    mov cx,5
    mov si,9   
    
    mov num_cx,cx
    
    mov ax,buf[si-1] 
    
    Label_loop:  
    
        mov  num_cx_1,cx
        mov cx,num_cx
        dec cx  
        
      Label_in:  
       
        sub si,2  
        mov bx,buf[si-1]
        cmp ax, bx
        jg Label_xchg 
        jmp Label_end  
        
      Label_xchg:  
     
        xchg ax,bx
        mov buf[si-1],bx
        mov buf[si+1] ,ax 
         
       Label_end :
        
        dec cx
        jnz  Label_in
        mov cx,num_cx_1
        dec cx
        jnz Label_loop   
        
    mov si,0
    mov cx,5   
    ;Output the answers....
    Label_output:
        mov dl,buf[si+1]
        add dl,30h
        int 21h   
        
        mov dl,buf[si]
        add dl,30h
        int 21h
        
        mov dl,2fh
        mov ah,2
        int 21h   
        
        add si,2
        dec cx
        jnz Label_output
     mov ah,4ch
     int 21h
        
        
code ends
end start
         
     
    

我来回复

您尚未登录,请登录后再回复。点此登录或注册