; from disc # 5
; --------------------------------------
; - direct-selection sorting algorithm -
; - optimized version by JPN/Level 4   -
; --------------------------------------
org	$40000
load	$40000

num_elements=30
a:
		lea	data_list,a0	; table for elements
		moveq	#00,d0		; outer loop counter (x)
outer:	
		move.w	d0,d1		; inner loop counter (y)
		move.w	d0,d2		; index for smallest (j=x)
		move.w	(a0,d2.w),d3	; a(j) -> d3		
inner:
		cmp.w	(a0,d1.w),d3	; a(y) < a(y) ???
		blt.s	smaller
		move.w	d1,d2		; j=y
		move.w	(a0,d2.w),d3	; a(j) -> d3			
smaller:
		addq.w	#02,d1		; next y value
		cmp.w	#num_elements*2,d1
		bne.s	inner		; next y
		move.w	(a0,d0.w),d4	; a(x) => d4
		move.w	d3,(a0,d0.w)	
		move.w	d4,(a0,d2.w)
		addq.w	#02,d0		; next x value
		cmp.w	#num_elements*2,d0
		bne.s	outer
		rts			

data_list:
	dc.w 5,2,3,1,6,8,9,7,4,0
	dc.w 5,2,3,-1,6,8,9,7,4,0
	dc.w 5,2,3,1,6,8,9,7,4,0
list_end:


