0

我正在开发一个排序程序,它将输入输入到一个数组中。我已经制作了最小值、最大值和平均值。现在我需要进行中位数、众数和排序(从最大值到最小值,从最小值到最大值)。

这是我得到的排序代码[更新的新代码]

       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
4

3 回答 3

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

我所做的只是将 Datatype 转换为从 String 到 Single 的最后一列,然后像以前一样比较相邻的值。

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

于 2014-04-23T22:22:07.363 回答
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

一旦您完全理解了上述内容,将其反转以进行降序排序应该不难。

于 2012-12-17T15:26:21.323 回答
0

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

使用 LINQ 计算基本统计信息

(包括方差、标准偏差、中位数、众数等的代码)

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

于 2012-12-17T15:52:56.383 回答