在VB中对数组进行排序而不使用排序函数

时间:2012-12-13 13:50:18

标签: vb.net

我正在开发一个排序程序,它将输入输入到数组中。我已经制作了Min,Max和Average。现在我需要进行中位数,模式和排序(最大到最小,最小到最大)。

这是我为排序[更新的新代码]而获得的代码

       RichTextBox1.Text = RichTextBox1.Text.Replace(" ", ",")


    marks = RichTextBox1.Text.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
    Label3.Text = Nothing

    Dim z As Integer = marks.Length - 1
    Dim y As Integer
    Dim TEMP As Integer

    For X = 1 To z
        For y = 1 To (z - 1)

            If marks(y) > marks(y + 1) Then
                TEMP = marks(y)
                marks(y) = marks(y + 1)
                marks(y + 1) = TEMP

            End If
            Label3.Text = Label3.Text & vbCrLf & marks(y)
        Next y


    Next X

3 个答案:

答案 0 :(得分:0)

这是维基百科转换为VB.net的选择排序算法

Public Shared Function Sort(ByVal a As Int32()) As Int32()
    Dim n As Int32 = a.Length ' a(n-1) is the last element
    Dim i As Integer, j As Integer
    Dim iMin As Integer

    ' Advance the position through the entire array 
    ' we could do "from j = 0 to n-2" (that is "for j < n-1")
    ' because single element is also min element
    For j = 0 To n - 2
        ' Find the min element in the unsorted a[j .. n-1] 

        ' Assume the min is the first element 
        iMin = j
        ' Test against elements after j to find the smallest 
        For i = j + 1 To n - 1
            ' If this element is less, then it is the new minimum 
            If a(i) < a(iMin) Then
                ' Found new minimum, remember its index 
                iMin = i
            End If
        Next

        ' iMin is the index of the minimum element,
        ' swap it with the current position 
        If iMin <> j Then
            Dim tmp As Int32 = a(j)
            a(j) = a(iMin)
            a(iMin) = tmp
        End If
    Next

    Return a
End Function

一旦你完全理解了上述内容,反转它进行降序排列并不困难。

答案 1 :(得分:0)

如果您不喜欢Sort,可以使用OrderBy。 LINQ还允许您直接计算MinMaxAverage,因此您无需重新发明轮子。有关更多信息,请参阅此文章:

Using LINQ to Calculate Basic Statistics

(包括Variance,StandardDeviation,Median,Mode等代码)

注意:代码是C#,但它可以很容易地转换为VB.NET,因为它们都是.NET语言。

答案 2 :(得分:0)

要对多维数组进行排序,您可以使用与排序单维数组完全相同的过程。

你循环遍历数组,如果两个相邻的成员顺序错误,那么你将它们四处翻转。

想象一下,您有一个多维数组,例如

Dim Array(12, 4) String

因此,假设您想按最后一列排序数组。

For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If Array(index2, 4) > Array(index2 + 1, 4) Then

// this sorts the last column

Temp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4) 
Array(index2 + 1, 4) = Temp4

// and we repeat this swopping pattern for each of the other columns, so they are all sorted in  the same way

Temp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3) 
Array(index2 + 1, 3) = Temp3

Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2) 
Array(index2 + 1, 2) = Temp2

Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1) 
Array(index2 + 1, 1) = Temp1

Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0) 
Array(index2 + 1, 0) = Temp0

Next
Next

所以现在所有列都按最后一列排序。

您可能想知道如果4列是字符串而一列是数字,您可以如何排序,并假设您想按数字列排序。

你可以这样做

For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If CSng(Array(index2, 4)) > CSng(Array(index2 + 1, 4)) Then

// this sorts the last column

Temp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4) 
Array(index2 + 1, 4) = Temp4

// and we repeat this swopping pattern for each of the other columns, so they are all sorted in the same way

Temp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3) 
Array(index2 + 1, 3) = Temp3

Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2) 
Array(index2 + 1, 2) = Temp2

Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1) 
Array(index2 + 1, 1) = Temp1

Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0) 
Array(index2 + 1, 0) = Temp0

Next
Next

我所做的就是将数据类型从String转换为Single的最后一列,然后像以前一样比较相邻的值。

因此,这是对多维数组进行排序的一种非常基本的方法,它适用于包含字符串和数字的混合数组。