Bubble Sort is a fundamental sorting algorithm that sorts by comparing and swapping adjacent elements. Below are the steps to implement bubble sort in a PLC ladder diagram.
1. Control Requirements
-
Input the data to be sorted in the numeric boxes D110-D119 on the touchscreen, totaling 10 items.
-
After sorting, the data will be stored in ascending order in the numeric boxes D120-D129.
2. Program Design
-
Use two nested for loops to complete the sorting within one scan cycle.


-
The specific program is as follows:
// Initialize variables
MOV D110 K0 M0
MOV D111 K0 M1
MOV D119 K0 M9
// Bubble sort logic
FOR i = 0 TO 8
FOR j = 0 TO 8 – i
CMP D[j] D[j+1]
JC MP001
// Swap data
MOV D[j] M10
MOV D[j+1] D[j]
MOV M10 D[j+1]
MP001: NOP
NEXT j
NEXT i
// Store the sorted results in D120-D129
MOV D0 D120
MOV D1 D121
……
MOV D9 D129
3. Verify Results
-
After completing the program, verify the sorting results through simulation to ensure correctness.
-
Ensure that the data is arranged in ascending order in D120-D129.
4. Best Practices and Tips
-
In practical engineering, it is common to use the built-in sorting functions of the PLC, eliminating the need to manually write sorting algorithms.
By following the above steps, bubble sort can be implemented in a PLC ladder diagram, suitable for simple sorting needs.