代码 Efficiency/Optimaization 和排序二维数组 VB6

Code Efficiency/Optimaization and Sort 2D Array VB6

我可以得到一些帮助来优化我的程序吗?我正在努力使我的代码尽可能高效,而且我还需要帮助对二维数组进行排序。

程序分析

编写一个程序,接收跳台滑雪选手名单和他们的分数。 该列表随后将存储在外部文件中。 对于每个跳投者,删除最高分和最低分,然后计算剩余分数的总和,将这个总分与跳投者的名字一起输出到一个新的外部文件中 包含参赛者分数的文件将从最高到最低排序。 必须包含一个函数。

代码:

Private Sub cmdStart_Click()

'Declare Variables used to Store Values

Dim Names(5) As String

'2D Array used to Display Score
'Value 1 represents number of competitor, while value 2 is ScoreNumber
'i.e Score(2,5) Represents Score 5(out of 5) of competitor 2( out of 5)
'Sixth Value in Array used to store Total Score

Dim Score(5, 8) As Integer
Dim CompetitorPointer As Integer
Dim counter As Integer
Dim Filename As String


'Input Scores for each Competitor into an Array
counter = 1
For counter = 1 To 5
    Names(counter) = InputBox("What is the Name of Competitor " & counter & " ?")
        For scorecounter = 1 To 5
            Score(counter, scorecounter) = InputBox("What is score " & scorecounter & " of Competitor " & counter & " ?")
        Next
Next


For CompetitorPointer = 1 To 5
    maximum = FindMax(CompetitorPointer, Score)
    minimum = FindMin(CompetitorPointer, Score)
    Score(CompetitorPointer, 7) = maximum
    Score(CompetitorPointer, 8) = minimum
Next

'Find total Score of Competitor
For CompetitorPointer = 1 To 5
    CompetitorScore = 6
    TotalScore = Score(CompetitorPointer, 1) + Score(CompetitorPointer, 2) + Score(CompetitorPointer, 3) + Score(CompetitorPointer, 4) + Score(CompetitorPointer, 5)
    Score(CompetitorPointer, CompetitorScore) = TotalScore
    MaxMin = Score(CompetitorPointer, 7) + Score(CompetitorPointer, 8)
    Score(CompetitorPointer, CompetitorScore) = TotalScore - MaxMin
Next


Min_Index = 1
Max_Index = 5

'File manipulation Program
'Create File

Open "Z:\AHComputing\VB\PROJECT\File.txt" For Output As #1
intMsg = MsgBox("File opened")

'Sorting Algorithm

'Selection sort is a simple sorting algorithm that mimics the way humans instinctively sort.
'It works by first scanning the entire list to find the smallest element, swapping it into the first position.
'It then finds the next smallest element, swapping that into the second position, and so on until the list is sorted.

    Dim i As Long
    Dim j As Long
    Dim iMin As Long
    Dim iMax As Long
    Dim varSwap As Variant

For CompetitorPointer = 1 To 5
    Min = 1
    Max = 5
    For i = Min To Max - 1
        iMin = Score(CompetitorPointer, i)
        For j = (i + 1) To 5
            If Score(CompetitorPointer, j) < iMin Then iMin = Score(CompetitorPointer, j)
        Next
        varSwap = Score(CompetitorPointer, i)
        Score(CompetitorPointer, i) = iMin
        iMin = varSwap
    Next

    Print #1, Names(CompetitorPointer), Score(CompetitorPointer, CompetitorScore)
Next CompetitorPointer


Close #1

intMsg = MsgBox("File closed")

End Sub

'Used to Find Highest Score of Competitor
Private Function FindMax(CompetitorPointer As Integer, ByVal Score)
    maximum = Score(CompetitorPointer, 1)
scorecounter = 1
For scorecounter = 1 To 5
    If Score(CompetitorPointer, scorecounter) > maximum Then
    maximum = Score(CompetitorPointer, scorecounter)
    End If
Next
FindMax = maximum
End Function


'Used to Find Lowest Score of Competitors
Private Function FindMin(ByRef CompetitorPointer As Integer, ByVal Score)
    minimum = Score(CompetitorPointer, 1)
scorecounter = 1
For scorecounter = 1 To 5
    If Score(CompetitorPointer, scorecounter) < minimum Then
    minimum = Score(CompetitorPointer, scorecounter)
    End If
Next
FindMin = minimum
End Function

我能否在数组排序方面获得一些帮助?我能否获得有关优化代码以提高其效率的提示? 我正在阅读记录,我想知道它们是否更适合存储值?

请注意,评论仅供我个人使用,因此您可以忽略它们。 谢谢

我会将数据放入数据库文件(我使用 MS Access),然后您可以使用 SQL 语句进行排序、查找 max/min 值等。这工作起来非常快。您不需要 运行 访问,一切都可以在 VB6 中完成。您可以使用 Access 创建一个空的数据库文件,其中包含已定义的表和字段。其他一切都可以从 VB6 内部完成 约翰

为了排序,使用 ADO 和断开连接的记录集。您不需要硬盘上的数据库,它是全内存解决方案。它快速而健壮,没有实验。请参阅我的旧 post 关于 sorting with ADO and disconnected recosrdet 的完整示例代码。