一、算法稳定性
一般认为冒泡排序是稳定的,但事实上却与具体算法有关系,尤其是对于有重复元素的数字序列:(下为实际测试程序,变量未改动)
算法1——稳定
For I2 = 1 To 21 '排序
For J2 = 1 To 22 - I2
J_J = J2 + 1
If PAX(J2) > PAX(J_J) Then
PATEMP = PAX(J2): PAX(J2) = PAX(J_J): PAX(J_J) = PATEMP
PATEMP = PAA(J, J2): PAA(J, J2) = PAA(J, J_J): PAA(J, J_J) = PATEMP
End If
Next
Next
结果——
原顺序3 4 1 1 4 3 1 1 2 2 4 3 2 2 1 3 2 1 3 4 1 2
排序后1 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4
升序位3 4 7 8 15 18 21 9 10 13 14 17 22 1 6 12 16 19 2 5 11 20
可见,这一算法是稳定的。
算法2——不稳定
For I2 = 1 To 21 '排序
For J2 = I2 + 1 To 22
If PAX(I2) > PAX(J2) Then
PATEMP = PAX(I2): PAX(I2) = PAX(J2): PAX(J2) = PATEMP
PATEMP = PAA(J, I2): PAA(J, I2) = PAA(J, J2): PAA(J, J2)