冒泡排序和双层循环的理解

冒泡排序:

Dim liSort() As Short = {5, 3, 1, 7, 6, 11, 234, 23, 1, 543}

Dim j As Short = 0

Dim liTemp As Short = 0

 

第一层循环控制,一共要比较多少轮。总长度 – 1 ,因为最后剩下1位,不需要再进行比较。

第二层循环控制,每一轮要比较多少次。每比较完1轮,就冒泡得到了最大的数字。最大数字不需要再加入比较。

 

For i = 0 To liSort.Length - 2

    For j = 0 To liSort.Length - 2 - i

        If liSort(j) > liSort(j + 1) Then

              liTemp = liSort(j)

              liSort(j) = liSort(j + 1)

              liSort(j + 1) = liTemp

          End If

    Next

Next

猜你喜欢

转载自www.cnblogs.com/gongjin/p/9166852.html
今日推荐