一、冒泡排序由来
冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。
它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果顺序(如从大到小、首字母从Z到A)错误就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素列已经排序完成。
这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡***终会上浮到顶端一样,故名“冒泡排序”。
二、KRL实现冒泡排序
DEF SortArray(Array[]:OUT,nzEnd:IN,nzDir:IN )
;***************************************************
;* Customer : *
;* Roboter : *
;* Version : Vxxxxxx *
;* Roboter Nr. : xxxxxx *
;* Controller Nr: xxxxxx *
;* Autor : Andrew Wang *
;* Company : *
;* Department : *
;* Telephone : 86 000-0000-0000 *
;* Version : 1.0 *
;* Created : 12.12.2018 *
;* Modified : *
;* Project : *
;* Program Name : SortArray *
;* Sort an array as a certain direction; *
;**************************************************
DECL REAL Array[]
DECL REAL nrTemp
DECL INT nzI
DECL INT nzEnd
DECL INT nzIdx
DECL INT nzDir
CONTINUE
IF VARSTATE("Array[]")<>#INITIALIZED THEN
MsgQuit(" The sorting Array not initialized!")
HALT
ENDIF
CONTINUE
IF VARSTATE("nzEnd")<>#INITIALIZED THEN
MsgQuit(" The sorting End index not initialized!")
HALT
ENDIF
CONTINUE
IF VARSTATE("nzDir")<>#INITIALIZED THEN
MsgQuit(" The sorting direction not initialized!")
HALT
ENDIF
FOR nzI= 1 TO nzEnd
IF (nzDir<=0) THEN
nzIdx= IdxMin(Array[],nzI,nzEnd)
ENDIF
IF (nzDir>0) THEN
nzIdx= IdxMax(Array[],nzI,nzEnd)
ENDIF
nrTemp =Array[nzI]
Array[nzI] =Array[nzIdx]
Array[nzIdx] =nrTemp
ENDFOR
END
三、应用
1.找出数组中***大数的索引
DEFFCT INT IdxMax(Array[]:OUT,nzStart:IN,nzEnd:IN )
;***************************************************
;* Customer : *
;* Roboter : *
;* Version : Vxxxxxx *
;* Roboter Nr. : xxxxxx *
;* Controller Nr: xxxxxx *
;* Autor : Andrew Wang *
;* Company : *
;* Department : *
;* Telephone : 86 000-0000-0000 *
;* Version : 1.0 *
;* Created : 12.12.2018 *
;* Modified : *
;* Project : *
;* Program Name : IdxMax *
;* Return the index of the highest value of Array; *
;**************************************************
DECL INT nzStart
DECL INT nzEnd
DECL INT nzIdx
DECL INT nzIdxMax
DECL REAL Array[]
CONTINUE
IF VARSTATE("nzStart")<>#INITIALIZED THEN
MsgQuit(" The sorting start index not initialized!")
HALT
ELSE
IF nzStart <1 THEN
MsgQuit(" The sorting start index invalid:>=1!")
HALT
ENDIF
ENDIF
CONTINUE
IF VARSTATE("nzEnd")<>#INITIALIZED THEN
MsgQuit(" The sorting end index not initialized!")
HALT
ELSE
IF (nzEnd < 1) OR (nzEnd <nzStart) THEN
MsgQuit(" The sorting End index invalid!")
HALT
ENDIF
ENDIF
CONTINUE
IF VARSTATE("Array[]")<>#INITIALIZED THEN
MsgQuit(" The sorting Array not initialized!")
HALT
ENDIF
nzIdxMax=nzStart
FOR nzIdx=(nzStart+1) TO nzEnd
IF Array[nzIdx] > Array[nzIdxMax] THEN
nzIdxMax=nzIdx
ENDIF
ENDFOR
RETURN (nzIdxMax)
ENDFCT
2.找出数组中***小数的索引
DEFFCT INT IdxMin(Array[]:OUT,nzStart:IN,nzEnd:IN )
;***************************************************
;* Customer : *
;* Roboter : *
;* Version : Vxxxxxx *
;* Roboter Nr. : xxxxxx *
;* Controller Nr: xxxxxx *
;* Autor : Andrew Wang *
;* Company : *
;* Department : *
;* Telephone : 86 000-0000-0000 *
;* Version : 1.0 *
;* Created : 12.12.2018 *
;* Modified : *
;* Project : *
;* Program Name : IdxMin *
;* Return the index of the lowest value of Array; *
;**************************************************
DECL INT nzStart
DECL INT nzEnd
DECL INT nzIdx
DECL INT nzIdxMin
DECL REAL Array[]
CONTINUE
IF VARSTATE("nzStart")<>#INITIALIZED THEN
MsgQuit(" The sorting start index not initialized!")
HALT
ELSE
IF nzStart <1 THEN
MsgQuit(" The sorting start index invalid:>=1!")
HALT
ENDIF
ENDIF
CONTINUE
IF VARSTATE("nzEnd")<>#INITIALIZED THEN
MsgQuit(" The sorting end index not initialized!")
HALT
ELSE
IF (nzEnd < 1) OR (nzEnd <nzStart) THEN
MsgQuit(" The sorting End index invalid!")
HALT
ENDIF
ENDIF
CONTINUE
IF VARSTATE("Array[]")<>#INITIALIZED THEN
MsgQuit(" The sorting Array not initialized!")
HALT
ENDIF
nzIdxMin=nzStart
FOR nzIdx=nzStart+1 TO nzEnd
IF Array[nzIdx] < Array[nzIdxMin] THEN
nzIdxMin=nzIdx
ENDIF
ENDFOR
RETURN (nzIdxMin)
ENDFCT
了解更多产品相关信息请关注:库卡KUKA机器人一级代理商