Combo, just one side note...
Why do you want to use bubble sort. That is awful algorithm but usually very easy to implement. I think that insertion sort is better in most cases (although both have N^2 complexity).
Here's my implementation:
Code:
FUNCTION_BLOCK Sort_block
VAR_TEMP
i : INT;
j : INT;
save_var : INT;
temp_var : INT;
END_VAR
VAR
// Static Variables
array_unsorted : ARRAY[0..9] OF INT;
array_sorted : ARRAY [0..9] OF INT;
END_VAR
// now creating array of integers in descending order
FOR i:= 0 TO 9 BY 1 DO
array_unsorted[i] := 9 - i;
array_sorted[i] := array_unsorted[i];
;
END_FOR;
// Now sort operation is performed
FOR i:= 1 TO 9 BY 1 DO
// Statement Section
j := i - 1;
temp_var := array_sorted[j];
save_var := array_sorted[i];
WHILE (j >= 0) AND (temp_var > save_var) DO
// Statement Section
array_sorted[j + 1] := array_sorted[j];
j := j -1;
;
END_WHILE;
array_sorted[j + 1] := save_var;
;
END_FOR;
;
END_FUNCTION_BLOCK
For your problem, Peter's approach is very good. It provides modularity which is very good, especially in cases when you need to modify or change sort criteria.
Regards,
Pandiani