冒泡排序 - 到VB.NET

时间:2016-05-16 07:44:00

标签: pseudocode

如果我有以下伪代码,我是否需要在其上添加任何内容,如下所述。非常感谢您的帮助:

**repeat 
     swapped = false**

     for i from 1 <- N
            for j <- 0 to N - 1
               if a[j] > a[j + 1]
                  swap( a[j], a[j + 1] )
  **swapped = true
           end if**
         **end for
       until not swapped**

我所需要的线路是否必须在那里?例如,如果问题是在伪代码中写入冒泡排序算法&#39;我是否需要完全写出来(包括**项目)或没有它们可以吗?

我们需要学习&#39;绳索学习&#39;代码,显然代码越小越好,也越容易记住。

谢谢!

1 个答案:

答案 0 :(得分:0)

Sub Main()
                Dim Numlist() As Integer = {23435, 1, 433, 5234}
    'here you can use any numbers in any order
        Dim Count As Integer = 0
                Dim Swapvalue As Integer
                For ii = 0 To Numlist.Length - 2
                    For i = 0 To Numlist.Length - 2
                        Count += 1
                        If Numlist(i) > Numlist(i + 1) Then
                            Swapvalue = Numlist(i)
                            Numlist(i) = Numlist(i + 1)
                            Numlist(i + 1) = Swapvalue
                        End If
                    Next
                Next
                Console.WriteLine("Number of comparisons: {0}", Count)
                For i = 0 To Numlist.Length - 1
                    Console.WriteLine(Numlist(i))
                Next
        End Sub