Last modified on 18 April 2011, at 15:13

Programming:Bubble sort

Revision as of 15:13, 18 April 2011 by Octoate (Talk | contribs) (added bubble sort)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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

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
	ret
			
temp 		db 0