汇编语言——希尔排序
为了课设写得代码,在这作为记录,原理就不多叙述,代码供参考。
include vcio.inc
include io32.inc
.data
array dword 39,80,76,48,13,29,50,78,30,11,100,7,48,86
item dword ? ;步长因子
itemNum dword ? ;步长因子个数
flag dword ?
flag2 dword 0
before dword ? ;arr[i-d]
msg1 byte "请输入增量因子个数",13,10,0
msg2 byte "由大到小输入每个增量因子",13,10,0
msg3 byte "输入完毕",13,10,0
msg4 byte " , ",0
res byte "步长为%d %n",13,10,0
.code
main proc
mov eax,offset msg1
call dispmsg
call readuid ;输入步长因子个数
mov ecx,eax
mov itemNum,ecx
mov eax,offset msg2
call dispmsg
mov esi,0
again: call readuid ;输入步长因子
mov item[esi*(type item)],eax
inc esi
loop again
mov eax,offset msg3
call dispmsg
push lengthof array
push esi
call ShellSort
exit 1
main endp
ShellInsert proc ;ShellInsert(n,d) n为数组长度,d为步长因子
push ebp
mov ebp,esp
mov eax,[ebp+12] ;i=d+1
;inc eax
.while(eax<[ebp+8]) ;i<=n
sub eax,[ebp+12]
mov ebx,array[eax*4]
mov before,ebx
add eax,[ebp+12]
mov edx,array[eax*4]
.if(edx<before) ;if(arr[i]<arr[i-d])
mov esi,edx
mov flag2,esi ;flag = arr[i]
sub eax,[ebp+12]
mov ebx,eax ;j=i-d
add eax,[ebp+12]
mov esi,flag2
.while(ebx>=0 && esi<array[ebx*4]) ;while(j>=0&&flag<array[j])
mov esi,array[ebx*(type array)]
add ebx,[ebp+12]
mov array[ebx*(type array)],esi ;array[j+d]=arr[j]
mov esi,flag2
sub ebx,[ebp+12] ;抵消add
sub ebx,[ebp+12] ;j=j-d
jnc next
.break
next:
.endw
add ebx,[ebp+12] ;j+b
mov esi,flag2
mov array[ebx*(type array)],esi ;array[j+b]=flag
.endif
inc eax ;i++
.endw
pop ebp
ret 8
ShellInsert endp
ShellSort proc
push ebp
mov ebp,esp
mov ecx,0
.while(ecx<[ebp+8])
push item[ecx*(type item)] ;步长
push [ebp+12] ;数组长度
call ShellInsert
call showArray
inc ecx
.endw
pop ebp
ret 8
ShellSort endp
showArray proc
mov esi,0
.while(esi<lengthof array)
mov eax,array[esi*(type array)]
inc esi
call dispsid
mov eax,offset msg4
call dispmsg
.endw
call dispcrlf
ret
showArray endp
end main