; from disc # 5
; ----------------------------------------------------------------
; --- implementation of the direct-selection sorting algorithm ---
; --- done by JPN/Level 4-ASD in August 1990 (assembly version)---
; --- This is the non-optimized version                        ---
; --- program sorts data sized as words from 'data_list'       ---
; --- program processes 16 bit signed data (-32768 -> +32767)  ---
; --- 'num_elements' features the number of elements to sort   ---
; --- routine length : 64 ($40) bytes                          ---
; --- any questions? Call Germany: (0)631/43752 (KRIS)         ---
; ----------------------------------------------------------------

org	$40000
load	$40000

num_elements=100

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)
inner:					
		move.w	(a0,d1.w),d3		; a(y) -> d3		
		cmp.w	(a0,d2.w),d3		; a(j) < a(y) ???
		bgt	smaller			
		move.w	d1,d2			; j=y
smaller:				
		addq.w	#02,d1			; next y value
		cmp.w	#num_elements*2,d1	; end of inner loop ?
		bne	inner			; next y
		move.w	(a0,d2.w),d3		; a(j) -> d3
		move.w	(a0,d0.w),d4		; a(x) => d4
		move.w	d3,(a0,d0.w)		; exchange	
		move.w	d4,(a0,d2.w)		; the numbers (d3, d4)
		addq.w	#02,d0			; next x value
		cmp.w	#num_elements*2,d0	; end of outer loop
		bne	outer			; next x
		rts			
end_of_routine:

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,-2,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
	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
	dc.w 5,2,3,1,6,8,9,7,4,0
	dc.w 5,2,3,1,6,8,9,7,4,0



