Last modified on 27 January 2019, at 04:29

Programming:Bubble sort

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
again	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