【问题标题】:Sorting an array in descending order using visual basic in VS2012在VS2012中使用Visual Basic按降序对数组进行排序
【发布时间】:2018-05-21 13:54:51
【问题描述】:

我正在制作一个程序,该程序通过单击按钮在 ListBox 中随机生成 100 行 15 行的数字。我需要将它从最大到最小,从左到右跨行排序。我有一个冒泡排序,但它从最小到最大排序,并且只在第一列下排序。

这就是我生成数字的方式:

 Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
    'Number Generator
    Dim rn As New Random()
    Dim array(14) As Integer      
    Dim temp As Integer
    Dim st As String

    For y As Integer = 1 To 100
        For x As Integer = 1 To 15
            array(x - 1) = rn.Next(100, 1000)
        Next
        txtList.Items.Add(ats(array))
    Next
    st = st & vbNewLine

    Call sort()

    Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
        Using sw As New StreamWriter(fs)
            sw.WriteLine()
        End Using
    End Using

End Sub

Function ats(ar As Integer()) As String
    'FUNCTION for array to string seperated by comma
    Dim sb As New System.Text.StringBuilder
    For x As Integer = 0 To UBound(ar)
        If x = UBound(ar) Then
            sb.Append(ar(x).ToString)
        Else
            sb.Append(ar(x).ToString & ", ")
        End If
    Next
    Return sb.ToString
End Function

这就是我对它们进行排序的方式:

 Sub sort()

    'bubble sort from biggest to smallest 
    txtList.Sorted = True
    Dim array(14) As Integer
    Dim temp As Integer
    For ipass = 1 To UBound(array)
        For i = 0 To UBound(array) - 1
            If array(i) > array(i + 1) Then
                temp = array(i)
                array(i) = array(i + 1)
                array(i + 1) = temp
                array.Reverse()
            End If
        Next i
    Next ipass
End Sub

最后,这是我当前结果的一个示例:

107、512、139、233、582、460、698、231、395、724、717、284、699、419、825

119、214、513、382、538、161、431、603、573、354、757、307、204、906、200

124、493、153、507、675、878、698、911、625、171、915、174、270、629、770

126、585、480、317、731、193、385、143、152、374、246、124、205、347、936

139、497、422、381、127、968、236、637、406、758、594、944、929、733、428

任何帮助将不胜感激

【问题讨论】:

  • array.Reverse() 的用途是什么?
  • 我试图让它反转排序。打算把它拿出来,因为它完全没有改变结果。
  • Sub btnGen_Click 中的arraySort() 中的array 不同。您应该做的是将array 作为参数传递给Sort() 方法。 Wikipedia: Scope (computer science) 是相关的。

标签: vb.net visual-studio-2012


【解决方案1】:

这是使用专用类的替代解决方案。

Input() 方法接受一个布尔开关,允许按升序或降序对List(Of Integer) 进行排序。
Output() 方法将返回一个String() 数组,该数组可以使用其.AddRange() 方法传递给ListBox
Delimiter 属性可用于指定必须如何分隔字符串组件(在本例中为整数值)。

Private Class SortedLists
    Private OutputList As List(Of String)

    Public Sub New()
        OutputList = New List(Of String)
        Delimiter = ", "
    End Sub

    Public Property Delimiter As String

    Public Sub Input(Values As List(Of Integer), Ascending As Boolean)
        If Ascending Then
            Values.Sort()
        Else
            Dim IValues As IOrderedEnumerable(Of Integer) = Values.OrderByDescending(Function(i) i)
            Values = IValues.ToList()
        End If

        OutputList.Add(String.Join("", Values.
                              Select(Function(val, i) (val.ToString &
                              If(i < Values.Count - 1, Delimiter, "")))))
    End Sub

    Public Function Output() As String()
        Return OutputList.ToArray()
    End Function
End Class

重构过程创建随机整数列表,将字符串结果添加到ListBox 控件,将字符串保存到文件中。
整个过程经过的时间,用StopWatch计算,是7~9毫秒。
使用File.WriteAllLines(),经过的时间为10~14毫秒。

Dim rn As New Random()
Dim MySortedLists As New SortedLists
Dim MyIntegerList As New List(Of Integer)

For y As Integer = 1 To 100
    For x As Integer = 1 To 15
        MyIntegerList.Add(rn.Next(100, 1000))
    Next
    MySortedLists.Input(MyIntegerList, False)
    MyIntegerList.Clear()
Next

txtList.Items.AddRange(MySortedLists.Output())

'File.WriteAllLines is a little slower, but it's easier to read
File.WriteAllLines(My.Settings.DAT_PATH, MySortedLists.Output)


'Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
'    Using sw As New StreamWriter(fs)
'        For Each line As String In MySortedLists.Output
'            sw.WriteLine(line)
'        Next
'    End Using
'End Using

【讨论】:

  • 这种方式非常适合我的需要。谢谢!
【解决方案2】:

我按照你的“方式”做事。你应该知道有更好的方法来实现你想要的。

Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
    'Number Generator
    Dim rn As New Random()
    Dim array(14) As Integer
    Dim temp As Integer
    Dim st As String

    For y As Integer = 1 To 100
        For x As Integer = 1 To 15
            array(x - 1) = rn.Next(100, 1000)
        Next
        'txtList.Items.Add(ats(array))
    Next

    Dim sortedArray = sort(array)



    Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
        Using sw As New StreamWriter(fs)
            For Each item In sortedArray
                sw.WriteLine(item)
            Next
        End Using
    End Using
End Sub

Function sort(array() As Integer) As IEnumerable(Of Integer)

    'bubble sort from biggest to smallest 
    txtList.Sorted = True

    Dim temp As Integer
    For ipass = 1 To UBound(array)
        For i = 0 To UBound(array) - 1
            If array(i) > array(i + 1) Then
                temp = array(i)
                array(i) = array(i + 1)
                array(i + 1) = temp

            End If
        Next i
    Next ipass
    Dim sortedArray = array.Reverse()
    Return sortedArray
End Function

【讨论】:

  • 我正在尝试您的修改。当我注释掉这一行“txtList.Items.Add(ats(array))”时,单击按钮时列表框显示为空白。如果我取消注释它,我会回到排序顺序的第一格。我确信有更好的方法可以做到这一点,但我对编程和学习非常陌生。
  • 为什么使用array.Reverse() 而不是简单地反转冒泡排序中的比较?
猜你喜欢
  • 2014-02-16
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 2021-12-28
  • 1970-01-01
相关资源
最近更新 更多