(AB) Bubble Sort Ladder Logic Example Code

Binary insertion sort is straightforward (e.g. see here), with the shift as a single step via the COP instruction.

It would have to work backwards from the top of the array, because overlapping COP forward overwrites all values in a sequence after the first.


But I still think @5618's example is the best here.
 
Quick Sort it seems is not that quick when there is duplicate data.

Whereas Insertion Sort is probably a better allrounder.

A sample implementation of Insertion Sort in ST:
Code:
SIZE(Array_In, 0, ArraySize);
COP(Array_In[0], Array_Sorted[0], ArraySize);

FOR i := 1 TO ArraySize - 1 DO
    j := i;
    WHILE j > 0 AND ((Direction = 0 AND Array_Sorted[j].Count > Array_Sorted[j - 1].Count) OR (Direction = 1 AND Array_Sorted[j].Count < Array_Sorted[j - 1].Count)) DO
		cop(Array_Sorted[j], temp, 1);
		cop(Array_Sorted[j - 1], Array_Sorted[j], 1);
		cop(temp, Array_Sorted[j - 1], 1);
        j := j - 1;
    END_WHILE;
END_FOR;
 
I think you guys need to go back & read the original OP's post, he has an AOI for bubble sort but does not have licence for ST so wants it in ladder or FBD.
 
My idea isn't perfect, but it's simple and reasonably robust. There is a chance of mixing up or losing a recipe change if an edit to any element besides the .Integer being sorted is written at the moment of the three COPs swapping the edit in progress recipe.

That's pretty dependent on how recipe edits are made. If the sorted UDT.Integer is the last item changed in a recipe update, that shouldn't be a problem.

If edits are HMI made or if recipes are HMI viewed, how will the interaction be affected by the sorting of the UDT? It might be worthwhile to set a bit when opening an edit window that inhibits the sort until the window is closed.

I'm pretty sure Index has to be a DINT for indirect addressing. As written, I was thinking of preventing "What happens if I make index 33? Oh, the run light turns red and everything stops, whoops!" If you're worried about "What happens if I make Index 2147483647?" you could parallel a <0 with the >23. ;)
 
My idea isn't perfect, but it's simple and reasonably robust. There is a chance of mixing up or losing a recipe change if an edit to any element besides the .Integer being sorted is written at the moment of the three COPs swapping the edit in progress recipe.

That's pretty dependent on how recipe edits are made. If the sorted UDT.Integer is the last item changed in a recipe update, that shouldn't be a problem.

If edits are HMI made or if recipes are HMI viewed, how will the interaction be affected by the sorting of the UDT? It might be worthwhile to set a bit when opening an edit window that inhibits the sort until the window is closed.

I'm pretty sure Index has to be a DINT for indirect addressing. As written, I was thinking of preventing "What happens if I make index 33? Oh, the run light turns red and everything stops, whoops!" If you're worried about "What happens if I make Index 2147483647?" you could parallel a <0 with the >23. ;)

You're right, the less moving parts the better.
In cases where a one-pass sort is required or when dealing with a sizable array, it may be worth considering Quick-Sort or Insertion Sort as alternative sorting algorithms.

It is advisable to employ index limits when utilizing indirect addressing. Moreover, incorporating an appropriate fault routine can be beneficial as well.

Here's an example where an index overrun occurs but it is handled by the fault routine.
IndexOverRun.png

https://youtu.be/pTp5mUb98HA
 

Similar Topics

Hello, I been trying to sort some values in the the Do-more PLC but still have some issues. I have 20 elements from the data view with random...
Replies
9
Views
3,667
Hi, im creating compressor station project with S7-1200. Project requires that 4 compressors should work in cascade - that's already done, and...
Replies
13
Views
11,621
Hey, I have written a function in SCL for doing the bubble sort thing. The code is taken from wikipedia. But, question I have is: When you...
Replies
20
Views
6,158
Hi all, I need some help here please. I'm trying to understand the SCL programming example included in the SIEMENS SCL official documentation...
Replies
1
Views
7,828
Good afternoon guys, I have a basic question on the bubble level in the picture attached. This is probably really easy, but I don't get it, haha...
Replies
11
Views
3,360
Back
Top Bottom