代码效率/优化和排序2D阵列VB6

时间:2015-03-23 15:10:14

标签: arrays sorting optimization vb6

我可以帮助优化我的程序吗?我试图让我的代码尽可能高效,我还需要帮助对二维数组进行排序。

计划分析

编写一个程序,其中将列出跳台滑雪竞赛者及其分数。 然后,该列表将存储在外部文件中。 对于每个跳线,删除最高和最低分数,然后计算剩余分数的总和,将此总数与跳线名称一起输出到新的外部文件中 包含竞争对手分数的文件将从最高到最低排序。 必须包含一个功能。

CODE:

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

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

请注意,评论包含在我个人用途中,因此您可以忽略它们。 感谢

2 个答案:

答案 0 :(得分:0)

我会将数据放入数据库文件(我使用MS Access),然后您可以使用SQL语句进行排序,查找最大/最小值等。这非常快。你不需要运行Access,一切都可以在VB6内部完成。您可以使用Access创建一个定义了表和字段的空数据库文件。其他一切都可以在VB6内部完成 约翰

答案 1 :(得分:0)

要进行排序,请使用带有断开记录集的 ADO 。您不需要硬盘上的数据库,它是全内存解决方案。它快速而强大,无需实验。请参阅我的旧文章sorting with ADO and disconnected recosrdet,其中包含完整的示例代码。

相关问题