****************************************************************************
* THIS IS NOT A STANDALONE SOURCE!
* You can compile and assemble it, but you can't run it!!!
*
* The procedure below executes the sorting on a vector of 32-bit signed
* integers using the QuickSort algorythm.
*
* Note that this is meant to be an example only, so optimization has been
* sacrificed to enhance the readability (BTW: ESA isn't indicated at all
* for this kinda things...).
*
* To use it you need an integers vector of any length.
****************************************************************************

****************************************************************************
* QuickSort 1.0.0
****************************************************************************
* INFO	sorts in ascending order a vector of signed long integers
* SYN	QuickSort[VecAdr, LII, RII]
*	          a0      d0   d1
* IN	VecAdr	address of vector to sort	
*	LII	Leftmost Item Index (tipically 0)
*	RII	Rightmost Item Index (tipically number of
*	            integers-1)
* NOTE	vector items are .l
****************************************************************************

	procedure QuickSort[a0/d0-d1],d0-d5

 	move.l	d0,d4	;d4=L; d0=i
	move.l	d1,d5	;d5=R; d1=j

	move.l	d4,d2
	add.l	d5,d2
	lsr.l	#1,d2	;(L+R)/2
	move.l	(a0,d2.l*4),d2	;PVT

	repeat

	 while.s (a0,d0.l*4)<d2
	  addq.l	#1,d0	;increment i
	 ewhile
	 while.s (a0,d1.l*4)>d2
	  subq.l	#1,d1	;decrement j
	 ewhile

	 when.s d0<=d1	;if i<j
	  move.l	(a0,d0.l*4),d3
	  move.l	(a0,d1.l*4),(a0,d0.l*4)
	  move.l	d3,(a0,d1.l*4)	;VecAdr[i] <-> VecAdr[j]
	  addq.l	#1,d0	;increment i
	  subq.l	#1,d1	;decrement j
	 ewhen

	until.s d0>d1

	when.s d4<d1
	 QuickSort.s[sav:a0,d4,d1]
	ewhen
	when.s d0<d5
	 QuickSort.s[sav:a0,d0,d5]
	ewhen

	eproc
