Buubblesorting in Mervis

Hi,

I have written the below code as a function block for sorting 8 integer numbers.
The sorted numbers will be put in an array named H_S in ascending order.
The order of each input number will be put in an array named Rank. For example R[0] is the order of input 1, R[1] is the order of input 2, ... .
As an example if the 8 input numbers are [10,30,20,40,50,60,80] then
H_S=[10,20,30,40,50,60,70,80];
and
Rank=[1,3,2,4,5,6,7,8].
In the code, I, initially, set Rank=[1,2,3,4,5,6,7,8] and then using the code in the "If" section, I bubble-sort the ranks.
However, this code is not working properly and I am not getting the right responses.
I appreciate any help on this issue.


FUNCTION_BLOCK BubbleSort
VAR
H_S: ARRAY[0..7] OF INT;
Rank: ARRAY[0..7] OF INT:= [1,2,3,4,5,6,7,8];
I: int;
a,b,c,d: int;
END_VAR

VAR_INPUT
H1: int;
H2: int;
H3: int;
H4: int;
H5: int;
H6: int;
H7: int;
H8: int;
END_VAR
VAR_OUTPUT
R1: int;
R2: int;
R3: int;
R4: int;
R5: int;
R6: int;
R7: int;
R8: int;
END_VAR

H_S[0]:= H1;
H_S[1]:= H2;
H_S[2]:= H3;
H_S[3]:= H4;
H_S[4]:= H5;
H_S[5]:= H6;
H_S[6]:= H7;
H_S[7]:= H8;

Repeat
FOR I:= 1 TO 7 DO;
a:= H_S[I-1]; b:= H_S; c:= Rank[I-1]; d:= Rank;
IF H_S[I-1]>H_S THEN H_S[I-1]:= b; H_S:= a; Rank[I-1]:= d; Rank:= c;
END_IF;
END_FOR;
UNTIL H_S[0]<H_S[1] AND H_S[1]<H_S[2] AND H_S[2]<H_S[3] AND H_S[3]<H_S[4] AND H_S[4]<H_S[5] AND H_S[5]<H_S[6] AND H_S[6]<H_S[7]
END_REPEAT;

R1:= Rank[0];
R2:= Rank[1];
R3:= Rank[2];
R4:= Rank[3];
R5:= Rank[4];
R6:= Rank[5];
R7:= Rank[6];
R8:= Rank[7];
RETURN;
END_FUNCTION_BLOCK
 
Thread starter Similar threads Forum Replies Date
H Programmable Logic Controller - PLC 3
Top