Difference between revisions of "Programming:Bubble sort"

From CPCWiki - THE Amstrad CPC encyclopedia!
Jump to: navigation, search
(added bubble sort)
 
 
Line 10: Line 10:
  
 
bubble ld (temp), hl
 
bubble ld (temp), hl
ld ix, (temp)
+
again ld ix, (temp)
 
res FLAG, h
 
res FLAG, h
 
ld b, c
 
ld b, c

Latest revision as of 04:29, 27 January 2019

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