Simple Array Sort
How can I do this In SX/B for the· SX28. It's just a quick sorting routine.
Apparently I can't index an array this way.
What alternatives are there?
Thanks,
Eric
Sort: Do Flip=0 For Index = 0 to 7 If Sensor(Index)<Sensor(Index+1) Then Flip=1 Temp=Sensor(Index) Sensor(Index)=Sensor(Index+1) Sensor(Index+1)=Temp Endif Next Loop Until Flip=0 RETURN
Apparently I can't index an array this way.
What alternatives are there?
Thanks,
Eric
Comments
I think that if you create another variable that is one number higher than Index, say Index_Plus_1, you can implement this function almost as written. Here is what I am thinking should work. I have not tested it!
- Sparks
Thanks for your help,
Eric
Sort:
··Do
···Flip·=·0
···For·Index·=·0·to·7
····Index_Plus_1·=·Index·+·1
·····If·Sensor(Index)·<·Sensor(Index_Plus_1)·Then
········Flip·=·1
········Sensor(Index) = Sensor(Index) ^ Sensor(Index_Plus_1)
········Sensor(Index_Plus_1)·=·Sensor(Index_Plus_1) ^ Sensor(Index)
········Sensor(Index) = Sensor(Index) ^ Sensor(Index_Plus_1)
·····Endif
···Next
··Loop·Until·Flip·=·0
RETURN
The ^ operator is the XOR operator
regards peter
If you do not want to use another variable (and it could be a temporary variable you reuse later), will you consider using an assembly block?
If so, this may work for you.
This should be functionally the same as the SX/B statement “If Sensor(Index) < Sensor(Index + 1) Then” if such a statement were valid. I derived it by looking at the assembly source produced by SX/B for my previous suggested statement of “If Sensor(Index) < Sensor(Index_Plus_1) Then.” I noted where it referenced Index_Plus_1 and modified it to address Index and then added the “INC W” statement to increment that value by one. (My changes are in bold.)
Note that by replacing the If…Then statement with assembly code I confused the SX/B compiler when it encountered the unpaired ENDIF statement near the end of the function. To remedy this I commented out the ENDIF statement, replaced it with a “Sort_ENDIF” label and modified the CJAE statement to jump to that label instead. (All of this to save a variable... and yes, I would have done it myself if I had the need!)
I hope this helps. It seems to compile though I have done no other testing.
Peter… I was pondering your XOR suggestion for quite a while. Who comes up with this stuff?!
- Sparks
Back to the drawing board.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap used 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
SX-Video Display Modules www.sxvm.com
Post Edited (Bean (Hitt Consulting)) : 2/27/2007 12:55:41 PM GMT
Can you explain how the Aliasing is working. (For the Laymen)
That is exactly right.
sensorPlus1(0) is the same variable as sensor(1)
sensorPlus1(1) is the same variable as sensor(2)
and so on...
If you want to keep your "where from" array in step with the sensor array, just swap the elements in your swap routine.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap used 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
SX-Video Display Modules www.sxvm.com
·
Looks like I can move ahead a little futher tonight. [noparse]:)[/noparse] thanks for the help...Again, Like aways you pull through... Are you think of adding the Ability to Add
to SX/B?
Also Bean I sent you a PM on another topic.
Post Edited (SailerMan) : 2/27/2007 1:48:27 PM GMT
DEVICE SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ 20_000_000
flip········ VAR Bit
index······· VAR Byte
sensor······ VAR Byte (9)
sensorPlus1· VAR Sensor(1)
PROGRAM Start NOSTARTUP
Start:
· PUT Sensor, 5, 3, 7, 8, 2, 1, 6, 4, 9
Sort:
· DO
·· flip = 0··
·· FOR index = 0 TO 7
···· __PARAM3 = Sensor(index)
···· __PARAM4 = SensorPlus1(index)
···· IF __PARAM3 < __PARAM4 THEN
······· flip = 1
······· swap __WPARAM34
······· Sensor(index) = __PARAM3
······· SensorPlus1(index) = __PARAM4
···· ENDIF
·· NEXT
· WATCH sensor
· BREAK
· LOOP UNTIL flip = 0
END
regards peter
Each sensor has a number and even though the Sensors get sorted by their value I still need to know the sensor number.
Can anyone see the problem.
Thanks,
Eric
·Put Number,0,1,2,3,4,5,6,7,8··
Do
·Put Sensor,$00,$00,$00,$00,$00,$00,$00,$00,$00
to
Do
·Put Number,0,1,2,3,4,5,6,7,8
·Put Sensor,$00,$00,$00,$00,$00,$00,$00,$00,$00
otherwise, the 2nd time through the loop the sensor numbers have switched places,
which of course is not the case, since you didn't connect the sensors in a different order.
regards peter
Thanks for your response... I'm not at home to try this right now... But Darn I think this will do it. It's amazing how simple it is to miss the obvious.
Eric