2012-12-13 32 views
0

我正在处理一个排序程序,它将输入输入到一个数组中。我已经做出了最小值,最大值和平均值。现在我需要做中位数,模式和排序(最大到最小,最小到最大)。在VB中对数组进行排序而不使用排序功能

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

 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 
+0

为什么不能使用['Array.Sort'](http://msdn.microsoft.com/en-us/library/system.array.sort(v = vs.110).aspx)? –

+0

任务是做一个排序功能.. –

+0

请看这里http://en.wikipedia.org/wiki/Bubble_sort – Teejay

回答

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 

倒车它做一个一旦你完全理解了上述内容,降序排序不应该很难。

+0

谢谢@Teejay,我正在研究和理解你提供的代码。只是一个问题:我上面写的代码不工作吗?在逻辑上它似乎应该.. –

+0

@ Ds.109我测试了它,它似乎工作,但它打印了很多输出,你只需要考虑最后一个系列。 – Teejay

+0

对我来说,它只能打印20个左右的数字。输入{4 1 2 8 5} - 排序后的输出{1 2 5 0 1 2 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0} ...所以这是一种工作,有点不行。 –

0

如果您不喜欢Sort,您可以使用OrderBy。 LINQ还允许你直接计算Min,MaxAverage,所以你不需要重新发明轮子。见本文的详细信息:

Using LINQ to Calculate Basic Statistics

(包括方差,StandardDeviation,中值,众等码)

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

+0

他说:“任务是做一个排序功能..” – Teejay

+0

@Teejay:作业任务...好吧。那不好意思了。我会在这里留下答案,以防有人需要以最少的开发工作实现统计功能,即满足业务需求。 – Neolisk

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 

我所做的是把数据类型从字符串转换为最后一列单,然后根据之前相邻的值进行比较。

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