Hardware Reference
In-Depth Information
done
ldaa
sign,SP
; check to see if the original number is negative
beq
chkout
ldd
#$FFFF
; convert to two's complement format
subd
val,SP
; if the number is negative
addd
#1
;
''
std
val,SP
;
''
chkout
ldaa
err,SP
; check the error flag
beq
clrErr
; go to clear CY flag before return
sec
; clear the C flag
bra
dealloc
clrErr
clc
; set the C flag
dealloc
ldd
val,SP
leas
locVar,SP
; deallocate local variables
puly
; restore Y
rts
;
org
$FFFE
; needed under CodeWarrior
;
dc.w
start
;
''
end
4.6.2 Bubble Sort
Sorting is among the most common ingredients of programming and many sorting
methods are being used. Sorting makes many efficient search methods possible. The bubble
sort is a simple, widely known, but inefficient, sorting method. Many other more efficient
sorting methods require the use of recursive subroutine calls, which fall outside the scope of
this topic.
The basic idea underlying the bubble sort is to go through the array or file sequentially
several times. Each iteration consists of comparing each element in the array or file with its
successor (x[ i ] with x[ i 1 1]) and interchanging the two elements if they are not in proper order
(either ascending or descending). Consider the following array:
157 13 35 9 98 810 120 54 10 30
Suppose we want to sort this array in ascending order. The following comparisons are made
in the first iteration:
x[0] with x[1] (157 and 13) interchange
x[1] with x[2] (157 with 35) interchange
x[2] with x[3] (157 with 9) interchange
x[3] with x[4] (157 with 98) interchange
x[4] with x[5] (157 with 810) no interchange
x[5] with x[6] (810 with 120) interchange
x[6] with x[7] (810 with 54) interchange
x[7] with x[8] (810 with 10) interchange
x[8] with x[9] (810 with 30) interchange
Thus, after the first iteration, the array is in the order
13 35 9 98 157 120 54 10 30 810
Notice that after this first iteration, the largest element (in this case 810) is in its proper
position within the array. In general, x[ n 2 i ] will be in its proper position after iteration i . The
 
Search WWH ::




Custom Search