TITLE Program Template
;程序的描述:冒泡排序算法,使用交换标志
;作者:落叶树
;创建日期:16:04 2007-8-6
;版本:
INCLUDE Irvine32.inc
BubbleSort proto,pArray:ptr dword,Count:dword ;原型声明。
.data
ArraySize=50
array dword ArraySize dup (?)
.code
main PROC
call Randomize
mov edi,offset array
mov ecx,ArraySize
L1:
mov eax,ArraySize
call RandomRange
stosd
loop L1 ;生成0-arraysize范围内的随机数。
invoke BubbleSort,addr array,ArraySize
mov esi,offset array ;显示数组。
mov ecx,lengthof array
mov ebx,type array
call DumpMem
call Crlf
exit
main ENDP
;其他子程序
BubbleSort proc uses ecx edx esi edi,
pArray:ptr dword,
Count :dword
local xFlag:byte ;交换标志
mov ecx,count
dec ecx
;and xFlag,0
L1:
and xFlag,0 ;交换标志要放循环里面检测内循环是否有交换,默认置0
push ecx
mov esi,pArray
L2:
mov eax,[esi]
cmp eax,[esi+4]
jle L3 ;jle为顺序排列,jge为逆序排列。
xchg eax,[esi+4]
mov [esi],eax
or xFlag,1 ;如果有交换则置1。
L3:
add esi,4
loop L2
pop ecx
cmp xFlag,1 ;检测交换标志,内循环无交换则排序完毕直接退出。
jnz quit
loop L1
quit:
ret
BubbleSort endp
END main