Programming:Bubble sort

From CPCWiki - THE Amstrad CPC encyclopedia!
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
;
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