有排序的另一種方式知道作爲Selection Sort和這裏其:
數據段被定義爲:
elements db 7,1,0,5,'$'
size dw $-elements - 1 ; size stores 5 (including '$') so we subtract one now it stores 4 (which is the actual number of elements)
msg db 10,13,'Unsorted Array: $'
msg2 db 10,13,'Sorted Array: $'
過程進行排序的數組:
sort proc
mov cx, size
mov si, 0
mov di, 0
mov ax, 1 ; this is done
dec cx ; to avoid the last comparison
outerLoop:
push cx ; store the limit of outerLoop in stack
mov bl, elements[si] ; store the element in bl pointed by si
mov cx, size ; store the value of size in cx for innerLoop
sub cx, ax ; this is done so that the limit does not proceed the limit of array elements.
innerLoop:
inc di
cmp bl, elements[di] ; compare the if BL is not greater than
jna continue ; content of element pointed by DI if yes then continue otherwise swap the value by executing below statements
mov dl, elements[di] ; get the element pointed by DI into DL
mov elements[di], bl ; swap the
mov elements[si], dl ; elements
mov bl, dl ; store the smallest element in BL
continue:
loop inner
inc ax
pop cx ; get the limit of outerLoop
inc si ; increment the index of outerLoop to point to the next element
mov di, si
loop outer
return2:
ret
sort endp
end
例子e對於「泡泡」排序是[here](http://stackoverflow.com/a/26324630/3512216)。 – rkhb