VB排序算法
发布网友
发布时间:2022-05-23 19:57
我来回答
共1个回答
热心网友
时间:2023-11-19 10:25
2,3,5,15,68,55
13,5,6,87,2,8,98
55,145,4,621,54,31,
2,5,54,6345,66
8,123,54,64,12,5
4,5,46,5,12,5,5
6,1,2,54,1
1,2,45,41,23
5,1,56,31,45
我把TXT中的内容写成这样,应该是合乎你要求的吧?然后代码如下:
Option Explicit
Dim Arry() As String
Private Sub Command1_Click()
Dim i As Long, j As Long
Dim x As String, y As String
i = 1
ReDim Preserve Arry(i) As String
Open App.Path & "\111.txt" For Input As #1
While Not EOF(1)
Line Input #1, Arry(i)
i = i + 1
ReDim Preserve Arry(i) As String
Wend
ReDim Preserve Arry(i - 1) As String
Close #1
For i = 1 To UBound(Arry)
For j = 1 To UBound(Arry)
If j < UBound(Arry) Then
If Val(Arry(j)) > Val(Arry(j + 1)) Then
x = Arry(j)
y = Arry(j + 1)
Arry(j) = y
Arry(j + 1) = x
End If
End If
Next
Next
Open App.Path & "\222.txt" For Output As #1
For i = 1 To UBound(Arry)
Print #1, Arry(i)
Next i
Close #1
End Sub
经过上机测试,生成的222.TXT中的内容如下:
1,2,45,41,23
2,3,5,15,68,55
2,5,54,6345,66
4,5,46,5,12,5,5
5,1,56,31,45
6,1,2,54,1
8,123,54,64,12,5
13,5,6,87,2,8,98
55,145,4,621,54,31,
首位是排好序的。