PDA

View Full Version : Sorting arrays



mark_s
- 21st March 2012, 22:06
I am trying to sort paired ADC readings in two arrays. In a loop, an0 an1 are sampled and stored
in seperate arrays. These become matched pairs for that time period. I need the min/max for all the readings for AN0. No problem with one array. My problem is keeping the AN1 samples, paired with the original AN0 samples after sorting. If anyone has an approach, I would really appreciate
the help. Thanks
Example:
Sampled RAW data
Array
Pos AN0 AN1
1 512 514
2 512 512
3 998 326
4 1021 270
5 640 499
6 300 504
7 423 511
8 512 512
9 512 514
10 510 512
11 511 512
12 515 512

What I need below

SORTED AN0 IN ASSENDING ORDER, AN1 STAYS PAIRED WITH ORIGINAL AN0
New Pos AN0 AN1 Old Pos
1 300 504 6
2 423 511 7
3 510 512 10
4 511 512 11
5 512 514 1
6 512 512 2
7 512 512 8
8 512 514 9
9 515 512 12
10 640 499 5
11 998 326 3
12 1021 270 4

The code below is based on Melanie's, which appeared in post 4 of this thread
http://www.picbasic.co.uk/forum/showthread.php?t=6734

I hope this can be modified to do what I need



SAMPLE_AN0 WORD [12]
SAMPLE_AN1 WORD [12]
CounterA var BYTE
DataTemp var WORD
Getsamples:
For A = 1 to 12
adcin 0 SAMPLE_AN0[A]
adcin 1 SAMPLE_AN1[A]
NEXT A
Sortdata: 'THIS WILL SORT SAMPLES_AN0 not SAMPLE_AN1
CounterA=0
SortArrayLoop:
If SAMP_AN0[CounterA+1] < SAMP_AN0[CounterA] then
DataTemp = SAMP_AN0[CounterA]
SAMP_AN0[CounterA] = SAMP_AN0[CounterA+1]
SAMP_AN0[CounterA+1]= DataTemp
If CounterA > 0 then CounterA = CounterA - 2
endif
CounterA = CounterA + 1
If CounterA < 11 then goto SortArrayLoop
END

Demon
- 22nd March 2012, 01:56
Off the top of my head again, what if you concatenate the numbers?

510512
511512

No matter what you have in the last 3 positions, 510 will always be in front of 511.

Robert

Ioannis
- 22nd March 2012, 09:40
Maybe for every entry have an array of 4 words?

Sorting based on the second element of the array.

Ioannis

mark_s
- 22nd March 2012, 14:23
Thanks for the feedback.

Demon, not sure I follow your idea?

Ioannis, I think this is the way. An array of arrays.
Are you suggesting a 4 word array as a Long variable?

Every data base or logger must deal with this problem. So I'm sure there
is a standard way. Most examples show the data being processed in a
pc using excel or VB and not in a pic.

Thanks again

Darrel Taylor
- 22nd March 2012, 14:28
As you move the AN0 samples, move the AN1 samples the same way...

SAMPLE_AN0 WORD [12]
SAMPLE_AN1 WORD [12]
CounterA var BYTE
DataTemp var WORD
Getsamples:
For A = 1 to 12
adcin 0 SAMPLE_AN0[A]
adcin 1 SAMPLE_AN1[A]
NEXT A
Sortdata: 'THIS WILL SORT SAMPLES_AN0
CounterA=0
SortArrayLoop:
If SAMP_AN0[CounterA+1] < SAMP_AN0[CounterA] then
DataTemp = SAMP_AN0[CounterA]
SAMP_AN0[CounterA] = SAMP_AN0[CounterA+1]
SAMP_AN0[CounterA+1]= DataTemp
DataTemp = SAMP_AN1[CounterA]
SAMP_AN1[CounterA] = SAMP_AN1[CounterA+1]
SAMP_AN1[CounterA+1]= DataTemp
If CounterA > 0 then CounterA = CounterA - 2
endif
CounterA = CounterA + 1
If CounterA < 11 then goto SortArrayLoop
END

mark_s
- 22nd March 2012, 15:01
Darrel,

Thank you for the answer!

Regards

Ioannis
- 22nd March 2012, 16:01
As Darrel replied, there is no need for a long variable. Besides you have 4 discrete variable that you have to treat as a whole.

Ioannis