从键盘输入有正负号,有小数点的10个数字(数字位数可能很长,一个字或一个双字存储不下),对这十个数进行排序后输出,不知道谁有好点的方法.有没有代码没关系,关键是先有个算法分析,大家帮帮忙,谢谢了
我刚学汇编,复杂的程序还不敢尝试,这里做了个冒泡排序过程,c文件中可以调用,使用方法,在BC3.1或TC3的IDE中建个PRJ把ex.asm和Myex.c导入就行了,当然也可以用到汇编程序里去了,这里我就不写了。
; ex.asm
PUBLIC _bubble_sort
.model small
.186
.code
_bubble_sort PROC C array: word, s: word
;===========================================================
; int i,j,temp;
; for(i=0;i<=s;i++) {
; for(j=0;j<s-1;j++) {
; if(array[j]>array[j+1]) {
; temp = array[j];
; array[j] = array[j+1];
; array[j+1] = temp;
; }
; }
; }
;============================================================
push ax
push bx
push cx
push dx
push di
push si
mov ax, 0 ; set i = 0
mov dx, s ; i <= s
mov di, OFFSET array
mov si, di
L1:
cmp ax, dx
jae OUT_LOOP1
dec dx ; j < s - 1
mov bx, 0
L2:
push dx
cmp bx, dx
jae OUT_INNER_LOOP
push ax
push bx
push cx ; save cx, dx for mul instruction
mov cx, 2
push ax ; save ax
mov ax, bx
mul cx
mov bx, ax ; bx = bx * 2
pop ax
push di
push si
add di, bx ; locate array[j]
add bx, 2
add si, bx ; locate array[j+1]
mov bx, [di]
cmp bx, [si]
ja SWAP
pop si
pop di
pop cx
pop bx
pop ax
pop dx
inc bx
jmp L2
SWAP:
mov ax, [di] ; temp = array[j]
mov bx, [si]
mov [di], bx ; array[j] = array[j+1]
mov [si], ax ; array[j+1] = temp
pop si
pop di
pop cx
pop bx
pop ax
pop dx
inc bx
jmp L2
OUT_INNER_LOOP:
pop dx
inc dx
inc ax
jmp L1
OUT_LOOP1:
pop si
pop di
pop dx
pop cx
pop bx
pop ax
ret
_bubble_sort ENDP
END
/* myex.c */
#include <stdio.h>
#ifdef __cplusplus
#define EXT extern "C"
#else
#define EXT extern
#endif
EXT bubble_sort(int array[], int size);
int main() {
int i;
int a[] = {10,4,3,7,5,8,2,6,9,1};
bubble_sort(a,10);
for(i=0;i<10;i++)
printf("%3d",a[i]);
printf("\n");
getch();
return 0;
}