Help with Sorting in BlitzArrays

Blitz3D Forums/Blitz3D Beginners Area/Help with Sorting in BlitzArrays

_PJ_(Posted 2011) [#1]
I feel incredibly dumb!

I have an array of values which each represent an element in the array. I am wanting to obtain the correct sequence of elements according to which element has which value.

Sounds complicated? Well, despite my need for help, It shouldnt really be as complex as it sounds. I'm just having trouble getting my head around it.

To help explain the situation a bit clearer the code below should make it a bit clearer. The problem is centred around the algorithm in the LAST For/Next loop!


Graphics 800,600,32,6

SeedRnd 12 ; JUST FOR TEST PURPOSES!


;Here's my arrays,with 8 elements
Global ARRAY_DESTINATION[8]
Global ARRAY_SEQUENCE[8]

; This is just to ensure no duplicates in Array_Destination
Global UNUSED=255

;Populate the DESTINATION Array with some random values from 0-8 (Random seed has been forced for testing)
For n=1 To 8
	x=Rand(8)-1
	
	; We don't want any duplicates
	If ((UNUSED And (2^x))<>2^x)
		While ((UNUSED And (2^x))<>2^x)
			x=Rand(8)-1
		Wend
	End If		
	UNUSED=UNUSED-(2^x)
	
	;Populate Array element n-1 with the randomised value x+1
	ARRAY_DESTINATION[n-1]=x+1
	
Next

; Display the arrays:

Local AROrig$
Local ARDest$
Local ARSeq$

For n=1 To 8
	AROrig=AROrig+LSet(Str(n-1),4)+" | "
	ARDest=ARDest+LSet(Str(ARRAY_DESTINATION[n-1]),4)+" | "
Next
Print AROrig
Print ARDest



; I need Array Sequence to contain the destinations from each origin in order, for example:
; Destination[0] = 7
; Destination[7] = 2
; Destination[2] = 4
; Destination[4] = 3
;....

; I need to ensure that ARRAY_SEQUENCE[0]=7
; and that ARRAY_SEQUENCE[1]=2
; and that ARRAY_SEQUENCE[2]=4
; and that ARRAY_SEQUENCE[3]=3
;....

For n=1 To 8
	ARRAY_SEQUENCE[ARRAY_DESTINATION[n-1]]=n-1			; Fails!
	
	ARSeq=ARSeq+LSet(Str(ARRAY_SEQUENCE[n-1]),4)+" | "
Next

Print ARSeq



Kryzon(Posted 2011) [#2]
Hi Mal, I know it feels "righter" to keep the For loop with 1~8, but if this is going to have you adding "-1" or "+1" offset to every index related value then I think it's easier to just go directly to 0~7 and avoid having to add them at all.

This would avoid you having the error which I think is in this line:
ARRAY_SEQUENCE[ ARRAY_DESTINATION[n-1] ] = n-1 ;The line where you wrote "Fails!". The fixed version is below.

ARRAY_SEQUENCE[ ARRAY_DESTINATION[n-1] - 1 ] = n-1 ;Don't forget the "-1" offset to the value. This value was "x + 1", so we need to take one down.

Make your life simpler
- Brucey.

Bye!

Last edited 2011


_PJ_(Posted 2011) [#3]
Thanks for looking, Kryzon. Seems that's not it, though or at least, not all there is to it.

I sorted out the issues with x+1 and changed the ForNext loop at the end to go from 0 to 7 to make things easier.
As you can see, I also had the brainstorm that by only doing a single iteration with no checking on the values, I was potentially missing some values which I THINK were getting overwritten in the original.

I'm still not getting the correct results, BUT all the values are now populated, and it seems it's just pulling them the "wrong way round"
Now it Should only be a simple (heh heh...) matter of rearranging the 'm' and 'n's to suit....

Wish me luck :)