我正在制作一个程序,使用按钮点击在ListBox中随机生成数字为100行15。我需要将它从LARGEST排序到SMALLEST,从左到右排成行。我有一个冒泡排序,但它排序最小到最大,只排在第一列。
这就是我获取数字的方式:
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
任何帮助将不胜感激
答案 0 :(得分:0)
我遵循了你做事的“方式”。你应该知道有更好的方法来达到你想要的目的。
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
答案 1 :(得分:0)
这是使用专用课程的替代解决方案。
Input()
方法接受一个布尔开关,允许按升序或降序对List(Of Integer)
进行排序。
Output()
方法将返回String()
数组,该数组可使用其ListBox
方法传递给.AddRange()
。
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