Help - Search - Members - Calendar
Full Version: Sorting two-dimentional arrays
Forums.MrPLC.com > PLCs and Supporting Devices > Allen Bradley
Contr_Conn
Got intersting question today about sorting 2-dimentional arrays, but can't find an easy solution.

Logix controller

Let say we have array DINT[x,y]
First we need to sort it based on first dimention only DINT[x,0] - this can be done with one SRT instruction.

Now difficult part:
As element DINT[x,0] goes up or down within array during sort,
it needs to grab all other elements within the same line
DINT[x,1], DINT[x,2]...DINT[x,y]
and move them at the same time.

To simulate this you can use excel:
Before
[attachmentid=3253]

Sort By column A
[attachmentid=3254]

After:
[attachmentid=3255]

I can do subroutine with buble sort using FOR-NEXT loop,
but for large arrays it will take some scan time.

Any ideas?
BobLfoot
QUOTE(Contr_Conn @ Oct 4 2006, 08:50 PM) [snapback]41227[/snapback]

Got intersting question today about sorting 2-dimentional arrays, but can't find an easy solution.

Logix controller

Let say we have array DINT[x,y]
First we need to sort it based on first dimention only DINT[x,0] - this can be done with one SRT instruction.

Now difficult part:
As element DINT[x,0] goes up or down within array during sort,
it needs to grab all other elements within the same line
DINT[x,1], DINT[x,2]...DINT[x,y]
and move them at the same time.

I can do subroutine with buble sort using FOR-NEXT loop,
but for large arrays it will take some scan time.

Any ideas?

Not Elegant but you are in a logix controller. Can you borrow a trick from msaccess and create an index to access the array and not move the data at all. Allow me to describe an example.
In your 2 dimensional array [15,0] has a value of 2 and is the smallest [x,0]. you load element [0,0] of the index array with 15 and element [0,1] iwth 2.
Element [32,0] is a 4 and next to smallest. index element [1,0] is set to 32 and [1,1] to 4.

Hope my idea is clear.
Contr_Conn
This is similar to what I did already with bubble sort, you still need (LEN-1)*(LEN-2) operations
It this will take too much time if we want to do it every scan
HEre is sorting example for [10,5] array

[attachmentid=3256]
TWControls
My method of doing this is not very good either. I increase the size of the array by one and duplicate the data I am sorting by. I then use the SRT instruction on one of them. Then I use an FSC to rearrange the other values.

Yep, it is pretty ugly

Edit - Didn't see your last, I had to do it in one scan once and the FOR was my final solution. Lots of watchdog issues but I sorted by 6 matching fields too
Contr_Conn
QUOTE(TWControls @ Oct 4 2006, 10:00 PM) [snapback]41236[/snapback]
My method of doing this is not very good either. I increase the size of the array by one and duplicate the data I am sorting by. I then use the SRT instruction on one of them. Then I use an FSC to rearrange the other values.

Yep, it is pretty ugly


TW: if two or more [x.0] elements have the same value, then how you will detemine which on you find with FSC?
Technically may be it does not matter as they all the same?

too late to think today...

TWControls
It didn't matter in my application because if the sort matched then all values matched. First thought would be put a 0 or something in the particular index after it is copied to the new position so the FSC wouldn't find again.

Edit - Of course if 0 is a possible value so that may not work. Since this method is wasteful of memory add another one to say if the index has been sorted or not
Sleepy Wombat
would a quick sort routine work in lieu of the bubble sort more efficiently ?
BobLfoot
CONN - What are you doing with the sorted data after the sort.

If you are wanting to sort and perform an operation based on the smallest or largest or midrange datum group each scan I think the following concept really has merit and will satisfy the watchdog.

Borrowing from TW how about this concept? Assume Array_Source is Dint[10,5]
1. COP [X,0] to Array_Index[X] for all 10 values.
2. SRT Array Index to get values in order.
3. Execute FSC on Array_Source looking at [X,0].
4. The position returned from the FSC is the element you want to act on.

This assumes you do not need the entire array resorted.
Contr_Conn
BobLFoot:
In this particular case we don't have to do it in one scan - I have no idea what customer does with data, but he needs it sorted onece in a while, so TW's method may work.

Edit:
We still need to execute FSC x-1 times

But in general someone may need sort to be performed within one scan.

Sleepy:
I heard about quick sort routine, but never try to implement it.
Do you have an example?
I know that buble sort if very un-efficient and takes too much scan time
but this is the only one I can program without looking in the book.
TWControls
Yes, the FSC is in the routine the FOR statement calls
QUOTE(Contr_Conn @ Oct 5 2006, 07:36 AM) [snapback]41256[/snapback]
We still need to execute FSC x-1 times
TWControls
I was hoping to have time to make a example of what I am thinking but have been too busy today.

This is as far as I have go and only gives the basic idea. It does not function completely. The FSC needs reset and the data needs to be copied to and from the original array. It would do it in a single scan but you could change the FOR to an ADD to make it process one step every scan

I will try to work more on it if I get some time
Contr_Conn
Will it be easier if we don't have to do it every scan?
I think yes, we can execute one FSC per scan...

Peter Nachtwey
The in place heap sort would be the fastest. The heap sort is very efficient plus you don't need to copy the data to a buffer to do the sorting.

It is not the simplest sort algorithm though.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.