当前位置:网站首页>CoDeSys realizes bubble sorting
CoDeSys realizes bubble sorting
2022-07-28 16:15:00 【Siege lion Yimu】
What is bubble sorting
Bubble sort of English Bubble Sort, It's a basic sort of exchange . It's called bubble sorting , Because every element can be like a small bubble , According to their own size, point by point to Array Move to one side of the .
The principle of bubble sorting :
Only one number can be determined in each trip . That is, the first trip can only confirm to return the number on the last bit , The second trip can only take the penultimate 2 The number on the bit returns , And so on . If there is n Number to sort , Just put the n-1 Return the number , That is to do n-1 Trip operation . and “ Every time ” It is necessary to compare two adjacent numbers from the first place , Put the larger number after , After the comparison, move back one bit and continue to compare the size relationship of the next two adjacent numbers , Repeat this step , Until the last number that hasn't returned .
How does it move ? Here's a reference ( Handsome bubble sort )
(1) At the beginning , The left subscript points to the first stone , The right subscript points to the second stone , Then compare

(2) Then move the left and right subscripts to the right at the same time , Compare again

(3) After the first trip , The largest stone moves to the far right

(4) Next, there is nothing left 3 Continue to choose the largest among the stones that are not arranged in order , The rules are the same as above

(5) This is given below 4 A complete demonstration process of stones

Don't talk much , With code on it :
The first is to define variables :
PROGRAM PLC_PRG
VAR
// bubble sort
xButton0 : BOOL ; // Sort start button
iBubbleSort1,iBubbleSort2 : INT ; // Bubbling cycle parameters
iSort_n,iSortNum1,iSortNum2 : INT ; // Sort auxiliary parameters
arrDim1 : ARRAY[0..2,0..8] OF INT ; // Tabular data
END_VAR
The following is the bubble sort program :
IF xButton0 THEN
FOR iSort_n := 0 TO 8 BY 1 DO
arrDim1[1,iSort_n] := arrDim1[0,iSort_n] ;
arrDim1[2,iSort_n] := arrDim1[0,iSort_n] ;
END_FOR
FOR iBubbleSort1 :=1 TO 8 BY 1 DO
FOR iBubbleSort2 :=0 TO (8-iBubbleSort1) BY 1 DO
IF arrDim1[1,iBubbleSort2] > arrDim1[1,iBubbleSort2+1] THEN // Ascending order
iSortNum1 := arrDim1[1,iBubbleSort2];
arrDim1[1,iBubbleSort2] := arrDim1[1,iBubbleSort2+1];
arrDim1[1,iBubbleSort2+1] := iSortNum1;
END_IF
IF arrDim1[2,iBubbleSort2] < arrDim1[2,iBubbleSort2+1] THEN // Descending order
iSortNum1 := arrDim1[2,iBubbleSort2];
arrDim1[2,iBubbleSort2] := arrDim1[2,iBubbleSort2+1];
arrDim1[2,iBubbleSort2+1] := iSortNum1;
END_IF
END_FOR
END_FOR
xButton0 := 0;
END_IFExecute the program through a button , Realize the sorting of numbers , Make a visualization file , That's how it works :

Let's do the following , Please see the effect after running :


After running, it realizes the ascending and descending arrangement of numbers ————
As well as Python Bubble sort program :
def bublle_sort(num):
for i in range(1,len(num)):
for n in range(0,len(num)-1):
if num[n] < num[n+1]:
num[n],num[n+1] = num[n+1],num[n]
return num
if __name__ == "__main__":
num = [1,3,2,8,4,6]
print(" The descending order is :",bublle_sort(num))Running results :
The descending order is : [8, 6, 4, 3, 2, 1]What about? , Does it feel codesys You can also make something different , I will continue to share different things with you in the future codesys, Let's industrial control people and play with the code , Next time I plan to use codesys Make an accumulator !!
边栏推荐
- Proportional solenoid valve control valve 4-20mA to 0-165ma/330ma signal isolation amplifier
- Shell programming specifications and variables
- 2021 Yahong pen test question 2
- js 链表 01
- The deep displacement monitoring system wk813 is used to measure the deep displacement of slopes, dams, embankments, railways and building foundation pit excavation
- R语言使用GGally包的ggpairs函数可视化分组多变量的两两关系图、设置alpha参数改变图像透明度、对角线上连续变量密度图、离散变量条形图、两两关系图中包含散点图、直方图、箱图以及相关性数值
- Connection and application of portable borehole inclinometer data acquisition instrument and inclinometer probe
- Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module
- 软考 系统架构设计师 简明教程 | 软件调试
- JS linked list 01
猜你喜欢

Thermistor PT100, NTC to 0-10v/4-20ma converter

0-75mV/0-100mV转RS485/232通讯接口MODBUS RTU采集模块IBF8

动态规划 --- 数位统计DP

Duty cycle switch output high speed pulse counter rtumodbus module ibf63

为什么学编程的人大多数都去了深圳和北京?

正大杯黑客马拉松数据解析竞赛

Two special functions (arrow function and method)

One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151

JS bidirectional linked list 01

JS linked list 02
随机推荐
NTC, PT100 thermal resistance to 4-20mA temperature signal converter
1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151
高速计数器转RS485Modbus RTU模块IBF150
Writing of factorial
阿里云的rds mysql 只读实例在哪里创建
js中的for循环总结
Common problems and precautions of remote serial port server (adapter) uart/i2c/1-wire/spi PS304
Application of optical rain gauge to rainfall detection
JS bidirectional linked list 01
正大杯黑客马拉松数据解析竞赛
12V脉冲转速测量转24V电平信号转换变送器
What are the process specifications of software testing? How to do it?
JS linked list 02
2021 Yahong pen test question 2
Solve the problem that the right-click menu "edit with idle" of the 『 py 』 file is invalid or missing
mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?
Data real-time feedback technology
Installation points and precautions of split angle probe
Rust Getting Started Guide (rustup, cargo)
食品安全 | 这两类瓜果宜改善便秘 孕妇人群尤其建议