比较来自世界各地的卖家的域名和 IT 服务价格

排序块状B. VB 不使用排序函数

我在排序程序上工作,在那里它将输入到数组。 我已经做到了 Min, Max 和 Average. 现在我需要做,中位,时尚和分类 /Max 到 min 和 min 到 Max/.

以下是我收到的代码进行排序[更新的新代码]


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
已邀请:

二哥

赞同来自:

这是一种分类选择的算法

维基百科

, 转变 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


翻过来以向下排序,一旦完全明白上面,就不应该困难。

裸奔

赞同来自:

如果你不喜欢
Sort

, 您可以使用
OrderBy

. LINQ 还允许我们计算
Min

,
Max


Average

直接,所以你不需要发明自行车。 有关更多信息,请参阅本文:

http://www.codeproject.com/Art ... stics
/包括分散代码, StandardDeviation, 中位数,政权等/

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

喜特乐

赞同来自:

用于排序 multidimensional array 您可以使用完全相同的步骤来对一个测量进行排序。

按阵列执行一个循环,如果两个相邻的元素以错误的顺序,则会在地方更改它们。

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


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, 然后比较相邻值,如前所述。

所以这是一种非常简单的方式 a multidimensional array, 它还适用于包含字符串和数字的混合阵列。

要回复问题请先登录注册