我分别用汇编和C++来递归实现冒泡排序
排20个完全逆序的数据,重复100000次
可是汇编运行时间为 220ms左右, C++运行时间为116ms 为何C++会更快呢
查看时间:
1.汇编通过调用DOS 2ch中断
2. C++ 通过前后输出系统时间相减
冒泡代码如下:
1.汇编
mov cx, 20;数组长度
dec cx
bubblesort proc near
push ax
push bx
push cx
mov bx, 0
_loop:
mov ax, array[bx]
cmp ax, array[bx+2]
jle continue
xchg ax, array[bx+2]
mov array[bx], ax
continue:
add bx, 2
loop _loop
pop cx
dec cx
cmp cx, 0
jz exit
call bubblesort
exit :
pop bx
pop ax
ret
2.C
void sortArray(int array[], int n, int length){
if (n == length) return;
int midint;
for (int j = length - 1; j > n; j--)
{
if (array[j] > array[j - 1])
{
midint = array[j];
array[j] = array[j - 1];
array[j - 1] = midint;
}
|