[Coco] quicker bubble sort?

Roger Taylor rtaylor at bayou.com
Tue Jan 20 00:46:49 EST 2004


Knowing that the largest value always floats to the very top on each pass, 
we can avoid having to scan the entire screen or table of data on each 
pass.  Instead, the table size is reduced by 1 byte on each pass.  The 
overhead looks to be insignificant.  I threw the extra code together so I'm 
sure there's a better way to do it.

* QUICKER BUBBLE SORT?
	org	10000
BUBSRT	clr	PassNo	set pass # to 0
	ldu	#1536	initial end of data pointer
bub010	leau	-1,u	reduce table size by 1
	stu	eotab
	ldx	#1024	point to screen
	ldy	#0	set 'change flag' to 0 (false)
bub020	lda	,x+	get first entry
	cmpa	,x	test next
	bls	bub030
	ldb	,x	get 2nd entry
	stb	-1,x	swap b to a
	sta	,x	swap a to b
	ldy	#1	set 'change flag'
bub030	cmpx	eotab	end of data?
	bne	bub020	no, keep sorting
	inc	PassNo	increment pass #
	cmpy	#0	test 'change flag'
	bne	bub010	sort again if change occured
loop	jmp	loop	branch here forever (must RESET the CoCo)
PassNo	fcb	0	pass # (initialized to 0)
eotab	fdb	1536	end of table pointer
	end	BUBSRT

----------
Roger Taylor






More information about the Coco mailing list