2017-03-09 31 views
0

我目前正在製作一個高分表 - 從一個.csv文件中讀取時間並從最低到最高排序它們。代碼運行後,列表只能部分排序。我的錯誤在這個視覺基礎Bubblesort

這是我輸入文件時間的方式,所以問題可能在這裏。

Private Sub BeginnerInput(ByRef player() As pe_player, ByRef x As Integer) 

    Dim path As String = "Z:\S6\Computing Science\PROJECT\Project\BeginnerLeaderboard.csv" 

    Dim leaderboard As StreamReader = New StreamReader(path) 

    Do While leaderboard.Peek() > -1 
     leaderboard.ReadLine() 
     x += 1 
    Loop 

    leaderboard.Close() 

    FileOpen(1, "Z:\S6\Computing Science\PROJECT\Project\BeginnerLeaderboard.csv", OpenMode.Input) 

    For i = 1 To x 
     Input(1, player(i).playerName) 
     Input(1, player(i).playerTime) 
     player(i).position = i 
    Next 

    FileClose() 

End Sub 

所有的數據輸入正確,但是當它進行排序時,它會錯誤地排列數據。如果我在.csv文件中有10個條目,它將排序四個條目,以便按順序排列另外四個條目,然後其餘的條目將排在底部,所以如果我有1,2,3,4,5,6,7, 8,9,10可以將它分類爲1-4-5-8-2-3-5-7-6-10。

Private Sub BeginnerProcess(ByRef player() As pe_player, ByVal x As Integer) 

    Dim i As Integer 
    Dim j As Integer 
    Dim temp As Object 

    For i = x To 0 Step -1 
     For j = 0 To i - 1 
      If player(j).playerTime > player(j + 1).playerTime Then 
       temp = player(j) 
       player(j) = player(j + 1) 
       player(j + 1) = temp 
      End If 
     Next 
    Next 

    Dim k As Integer 

    For k = 1 To x 
     player(k).position = k 
    Next 

End Sub 

下面是輸出 Leaderboard

+0

你有沒有通過您的代碼加強一個調試器,並尋找問題發生的地方? –

+0

是的,我找不到有斷點的錯誤 –

回答

0

適應經典的冒泡排序到你的情況,我想我應該像下面的代碼:

For i = 0 To x - 1 
    For j = i + 1 To x 
     If player(i).playerTime > player(j).playerTime Then 
      temp = player(i) 
      player(i) = player(j) 
      player(j) = temp 
     End If 
    Next 
Next 
+0

@ A.Colver是否在我的答案中嘗試了上面的代碼? –

+0

我確實嘗試過,但我仍然得到相同的結果。 –

+0

什麼是「玩家」?類數組?它是什麼類型? –