determine highest value

BlitzPlus Forums/BlitzPlus Programming/determine highest value

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

You can do it with a recursive function. It looks at the entire list, finds the smallest number, swaps it with the last position's number, then repeats with a list shortened by one, until it gets down to the last one, which shuld be the biggest number, at the top of the list.
; recursive sort
Dim sortlist(6)
For i = 1 To 6
	sortlist(i) = Rand(100)
	Print "Sortlist("+i+") = " + sortlist(i)
Next

sort(6)

Print "---"
For i = 1 To 6
	Print "Sortlist("+i+") = " + sortlist(i)
Next

Print "---"
For i = 1 To 6
	Print "Difference("+i+") = " + (sortlist(1) - sortlist(i))
Next
WaitKey

Function sort(depth)
	If depth = 1 Then Return
	low% = 1
	For i = 2 To depth
		If sortlist(i) < sortlist(low) Then low = i
	Next
	temp% = sortlist(depth)
	sortlist(depth) = sortlist(low)
	sortlist(low) = temp
	sort(depth - 1)
End Function