Sign in to follow this  
Followers 0
Luchek

Sorting algorithms

6 posts in this topic

Hi guys,

I'm using a Schneider Electric M340 PLC, I have a multi-dimensional array:
Array[1..10,1..10] of real. To give a little background. Each of those 10 arrays contains 6 parameters for a controller. They're run through a test and the score is recorded in the 7th element.

Now my problem is I need to sort those 10 arrays based on that 7th element.

More specifically I need to pluck the best two performing ones and write them to a new array set..

I've tried writing my own sorting algorithm although I only manage to take the first, best performing one as opposed to the top two.

This of course is programmed in ST.

Any advice would be appreciated,
Kind regards,
Max

Share this post


Link to post
Share on other sites

once you have determine first one (the best), store it someplace else, then set all of its elements to zero for example (in the list of 10). then repeat search for next best case....

1 person likes this

Share this post


Link to post
Share on other sites

.........that's smart... Can't believe I didn't think of that!

 

Thank you, legend.

Share this post


Link to post
Share on other sites

I guess one more question though..the way I determine the best is by just taking the MIN(value1, value2,etc...)

 

So If I set it to 0..it will be the best. Would setting it to 99999 work as well?

Share this post


Link to post
Share on other sites

that depends on your values. suppose you have values 1,2,3,4,5,6,7,8,9,10

if the "best" is smallest value, your first search will return "1". to prevent it from being selected again, set some large value such as 99999. this way next search will find "2" as a next best result.

if the "best" is largest value, your first search will return "10". to prevent it from being selected again, set it to some large negative value such as -99999. this way next search will find "9" as a next best result.

Share this post


Link to post
Share on other sites

you don't have to really modify original data, you can of course just mark it and exclude from next search. reason i mentioned modification of value is because you already have working code and this would allow it to run again without any code modifications.

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