"bubble sort" algorithm in SCL SIEMENS

buhnen

Member
Join Date
May 2008
Location
Somewhereland
Posts
131
Hi all,

I need some help here please. I'm trying to understand the SCL programming example included in the SIEMENS SCL official documentation. My doubt is about the "bubble sort" algorythm (comparing one to one elements within an array for sorting them in ascendent or descendent order).

I copy and paste the code and translate the variables(in spanish) to english:

BEGIN

REPEAT
change := FALSE;
FOR index := LIMIT TO 1 BY -1 DO
IF sorting_buffer[index-1] > sorting_buffer[index] THEN
help := sorting_buffer[index];
sorting_buffer[index] := sorting_buffer[index-1];
sorting_buffer[index-1] := help;
change := TRUE;
END_IF;
END_FOR;
UNTIL NOT change
END_REPEAT;


LIMIT takes the value of 8 (as my "sorting_buffer" array has 8 elements from 0 to 7).
The counter (index) decreases 1 each time checking 8 times the comparison between an element and the previous one. If the IF condition is satisfied both elements will be re-sorted and "change" will set to TRUE. IF NOT, again untill the rest of those 8 times, the checking of the IF will be repeated.

Let's say that the last time the FOR instruction executes it is made a comparison and the IF executes as well. Then, "change" will be set to TRUE and the IF and the FOR instructions reach to the end. We go to the next line of the program and see "UNTIL NOT change", so "Change" must be FALSE so that to close the REPEAT loop...but we have said that "change" was SET to true before.

I don't understand the reason to be for "change" and the REPEAT instruction. Could you please help me to understand this?
Thanks in advance.

P.S- I quicly "learnt" a year ago the basics of C language. I say it just in case my doubt can sound of a very newbie thing.
 
The FOR loop performs a comparison with adjacent elements and then swaps them as required. If a swap, or "change" is performed the array has not been sorted into order and the FOR loop has to be repeated. Eventually, when the FOR loop executes and no swaps (or changes) take place, the array is in order and the repeat loop needs to end.
 

Similar Topics

Hello all, I was looking into different sorting techniques and actually found a bubble/insertion sort sample code file on Rockwell's site. Only...
Replies
20
Views
5,245
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,655
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,606
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,153
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,344
Back
Top Bottom