mergesort代码中的StackOverflowException

时间:2018-06-20 21:25:26

标签: vb.net recursion stack-overflow mergesort

所以我目前正在学习A Level计算机科学,并且即将进入大学一年级。由于我们只有1年的时间,所以我们开始了我们的NEA项目,为了尽早开始,我现在而不是几个月后就开始了它,因为我的目标是进行A / A *。为了给您一些我正在做的项目的背景信息,这将是一个用程序VB.Net编码的气球塔防风格的游戏,并对我正在使用递归编码的合并排序的记分板进行排序(任何递归都会产生额外的分数) 。现在就给大家个提示,我对编码还很陌生,说实话,它可能已经变得可怕了,老实说,我并不真正知道,但是只是预警

这是整个排序算法代码:

Highscore是一种定义为数组的结构,其中Score为Integer,名称为String。

Sub AddNewScore(ByRef HighScore() As Score)
    'ByVal NewScore As Score
    'Test Purposes only Remove for fullbuild
    Dim TempName As String
    Dim TempScore As Integer
    Console.WriteLine("Enter the Name:")
    TempName = Console.ReadLine
    Console.WriteLine("Enter the Score")
    TempScore = Console.ReadLine

    If HighScore(9).Score < TempScore Then
        HighScore(9).Score = TempScore
        HighScore(9).Name = TempName
    End If
    'Test Purposes only Remove for fullbuild

    'If HighScore(9).Score < NewScore.Score Then
    'HighScore(9) = NewScore
    'End If
    MergeCore(HighScore)
End Sub
'Sorting Algorithm
Sub MergeCore(ByVal HighScore() As Score)
    Dim Left As Integer = 0
    Dim Right As Integer = 9
    Split(HighScore, 0, 9)
End Sub
Sub Split(ByRef HighScore() As Score, ByVal Left As Integer, ByVal Right As Integer)
    If Left < Right Then
        Dim Mid As Integer = (Left + Right) / 2
        Split(HighScore, Left, Mid)

        Split(HighScore, Mid + 1, Right)

        Merge(HighScore, Left, Mid, Right)
    End If

End Sub
Sub Merge(ByRef HighScore() As Score, ByVal Left As Integer, ByVal Mid As Integer, ByVal Right As Integer)
    Dim LSize As Integer = Mid - Left + 1
    Dim RSize As Integer = Right - Mid
    Dim LeftA(LSize) As Integer
    Dim RightA(RSize) As Integer
    Dim LPointer As Integer
    Dim RPointer As Integer
    For LP = 0 To LSize - 1
        LeftA(LP) = HighScore(Left + LP).Score
    Next
    For RP = 0 To RSize - 1
        RightA(RP) = HighScore(Mid + 1 + RP).Score
    Next
    LPointer = 0
    RPointer = 0
    Dim OriginalPointer As Integer = Left
    While LPointer < LSize And RPointer < RSize
        If LeftA(LPointer) <= RightA(RPointer) Then
            HighScore(OriginalPointer).Score = LeftA(LPointer)
            LPointer += 1
        Else
            HighScore(OriginalPointer).Score = RightA(RPointer)
            RPointer += 1
        End If
        OriginalPointer += 1
    End While
    While LPointer < LSize
        HighScore(OriginalPointer).Score = LeftA(LPointer)
        LPointer += 1
        OriginalPointer += 1
    End While
    While RPointer < RSize
        HighScore(OriginalPointer).Score = RightA(RPointer)
        RPointer += 1
        OriginalPointer += 1
    End While
    Console.WriteLine("ScoreBoard")
    For I = 0 To 9
        Console.WriteLine(HighScore(I).Name & " " & HighScore(I).Score)
    Next
    Console.WriteLine()
End Sub
End Module

错误("Exception of type System.StackOverFlowException has been thrown")在此代码的Sub Split行中:

   Sub Split(ByRef HighScore() As Score, ByVal Left As Integer, ByVal Right As 
  Integer)
    If Left < Right Then
        Dim Mid As Integer = (Left + Right) / 2
        Split(HighScore, Left, Mid)

        Split(HighScore, Mid + 1, Right)

        Merge(HighScore, Left, Mid, Right)
    End If

End Sub

我们将不胜感激任何帮助,对于其他任何建议,我们也将不胜感激

谢谢

0 个答案:

没有答案