Programming:Bubble sort

From CPCWiki - THE Amstrad CPC encyclopedia!
Revision as of 15:13, 18 April 2011 by Octoate (Talk | contribs) (added bubble sort)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Does a bubble sort (ascending) on a memory range.

; Usage: HL - address of table, C - number of bytes to sort
; call bubble
; Destroys: abcdefhlix

bubble	ld	(temp), hl
	ld	ix, (temp)
	res	FLAG, h
	ld	b, c
	dec	b
next	ld	a, (ix)
	ld	d, a
	ld	e, (ix + 1)
	sub	e
	jr	c, noswitch
xchange	ld	(ix), e
	ld	(ix + 1), d
	set	FLAG, h
noswitch	inc	ix
	djnz next
	bit	FLAG, h
	jr	nz, again
temp 		db 0