合并排序堆栈溢出vb.net

时间:2015-05-19 14:46:01

标签: vb.net stack-overflow mergesort

在我的项目中,我想在我项目的一个类的列表上进行合并排序:

首先,我为这个类做了比较的运算符

Public Class Programme

    Private DateLimite As Date


    Public Shared Operator >(ByVal p1 As Programme, ByVal p2 As Programme) As Boolean
         Return p1.acces_DateLimite > p2.acces_DateLimite
    End Operator

    Public Shared Operator <(ByVal p1 As Programme, ByVal p2 As Programme) As Boolean
         Return p1.acces_DateLimite < p2.acces_DateLimite
    End Operator

    Public Shared Operator >=(ByVal p1 As Programme, ByVal p2 As Programme) As Boolean
         Return p1.acces_DateLimite >= p2.acces_DateLimite
    End Operator

    Public Shared Operator <=(ByVal p1 As Programme, ByVal p2 As Programme) As Boolean
         Return p1.acces_DateLimite <= p2.acces_DateLimite
    End Operator

End Class 

之后,我制作了合并排序算法

Public Function InsertListe(element As Programme, sequence As List(Of Programme)) As List(Of Programme)
    Dim list = New List(Of Programme)
    If sequence.Count = 0 Then
        list.Add(element)
        Return list
    ElseIf element <= sequence.First Then
        sequence.Insert(0, element)
        Return sequence
    Else
        list.Add(sequence(0))
        list.AddRange(InsertListe(element, sequence.GetRange(1, sequence.Count - 1)))
        Return list
    End If
End Function


Public Function Merge(subSeq1 As List(Of Programme), subSeq2 As List(Of Programme)) As List(Of Programme)
    If subSeq1.Count = 0 Then
        Return subSeq2
    ElseIf subSeq2.Count = 0 Then
        Return subSeq1
    Else
        Return Merge(subSeq1.GetRange(1, subSeq1.Count - 1), InsertListe(subSeq1(0), subSeq2))
    End If
End Function


Public Function MergeSort(sequence As List(Of Programme)) As List(Of Programme)
    Dim n = sequence.Count
    If n = 0 Or n = 1 Then
        Return sequence
    Else
        Return Merge(MergeSort(sequence.GetRange(0, (n + 1) / 2)), _
                     MergeSort(sequence.GetRange((n + 1) / 2, n - ((n + 1) / 2))))
    End If
End Function

但是当我在'List(Of Program)'上调用它时,会抛出StackOverFlowException 我找不到原因。我正在查看调用堆栈,即使n大于1,也只调用''MergeSort''函数。

提前致谢。

1 个答案:

答案 0 :(得分:0)

Programme类替换为UInteger(只是简单数据类型的示例)和次要样式调整后,一切都适合我:

Public Shared Sub Test()
    Dim oPRNG As New Random
    For ndxTrial As Integer = 1 To 10
        Console.Out.WriteLine("Trial # {0}", ndxTrial)
        Dim nItems As Integer = oPRNG.Next(5, 25)
        Dim auValues As New List(Of UInteger)(nItems)
        For ndxItem As Integer = 0 To nItems - 1
            auValues.Add(CUInt(oPRNG.Next(0, 100)))
        Next
        Console.Out.WriteLine("Random: {0}", FormatList(auValues))
        Console.Out.WriteLine("Sorted: {0}", FormatList(MergeSort(auValues)))
        Console.Out.WriteLine()
    Next
End Sub

Public Shared Function FormatList(ByVal auValues As List(Of UInteger)) As String
    Return String.Format("{{ {0} }}", String.Join(", ", auValues))
End Function

Public Shared Function InsertListe(element As UInteger, sequence As List(Of UInteger)) As List(Of UInteger)
    Dim list As New List(Of UInteger)
    If sequence.Count = 0 Then
        list.Add(element)
        Return list
    ElseIf element <= sequence.First Then
        sequence.Insert(0, element)
        Return sequence
    Else
        list.Add(sequence(0))
        list.AddRange(InsertListe(element, sequence.GetRange(1, sequence.Count - 1)))
        Return list
    End If
End Function


Public Shared Function Merge(subSeq1 As List(Of UInteger), subSeq2 As List(Of UInteger)) As List(Of UInteger)
    If subSeq1.Count = 0 Then
        Return subSeq2
    ElseIf subSeq2.Count = 0 Then
        Return subSeq1
    Else
        Return Merge(subSeq1.GetRange(1, subSeq1.Count - 1), InsertListe(subSeq1(0), subSeq2))
    End If
End Function


Public Shared Function MergeSort(sequence As List(Of UInteger)) As List(Of UInteger)
    Dim n As Integer = sequence.Count
    If n = 0 Or n = 1 Then
        Return sequence
    Else
        Return Merge(MergeSort(sequence.GetRange(0, (n + 1) \ 2)), _
                     MergeSort(sequence.GetRange((n + 1) \ 2, n - ((n + 1) \ 2))))
    End If
End Function

因此,首先要解决的是浮点除法运算 - 使用返回斜杠(整数除法)而不是转发:这样你会保证适当的舍入。

与此相关,值得注意的是明确你的意图的重要性,而不是依赖于自动类型演绎。而不是

    Dim n = sequence.Count

    Dim n As Integer = sequence.Count

而不是

    Dim list = New List(Of Programme)

    Dim list As New List(Of Programme)

此外,始终启用Option StrictOption Explicit,以及在项目属性中禁用Option Infer。这将节省您调试神秘错误的时间。

总的来说,我希望这个“项目”只是你的语言学习而不是真正的应用程序,因为选择的算法和选定的容器(.Net中的List只是一个动态重新分配的数组)是超乎想象的次优