Sign in to follow this  
Followers 0
Contr_Conn

Sorting two-dimentional arrays

13 posts in this topic

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 Sort By column A After: I can do subroutine with buble sort using FOR-NEXT loop, but for large arrays it will take some scan time. Any ideas?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 Edited by Contr_Conn

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
would a quick sort routine work in lieu of the bubble sort more efficiently ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. Edited by Contr_Conn

Share this post


Link to post
Share on other sites
Yes, the FSC is in the routine the FOR statement calls

Share this post


Link to post
Share on other sites
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 Sort2D.ACD

Share this post


Link to post
Share on other sites
Will it be easier if we don't have to do it every scan? I think yes, we can execute one FSC per scan...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0