选择排序 - 堆栈溢出

时间:2010-10-20 17:36:00

标签: asp.net vb.net sorting recursion selection

您好我正在尝试使用递归在vb.net中实现Selection Sort。问题是如果我要排序的数组是1000个元素,我会不断获得stackoverflow。我找不到问题。我不太了解堆栈溢出或如何检查它。从我的研究中,问题可能是无限递归,但我检查并退出sub.Here是主要代码:

Public Class SelectionSort
Inherits DefaultSort


Public Sub New(ByVal num As Integer)
    Me.CreateArray(num)
    Me.ShowArray()
    Me.StartWatch()
    Me.Sort(Arr.Length - 1, 0, 1)
    Me.StopWatch()
    Me.ShowArray()
    Me.ShowExecutionTime()
End Sub





Private IsSorted As Boolean = False
Public Overridable Sub Sort(ByVal arrLen As Integer, ByVal pos As Integer, ByVal minval As Integer)



    If pos >= arrLen Then
        IsSorted = True
    End If

    If IsSorted = True Then
        Exit Sub
    End If



    Dim minKey As Integer = Array.IndexOf(Arr, minval + pos)
    Dim temp As Integer = Arr(minKey)


    Arr(minKey) = Arr(pos)
    Arr(pos) = temp
    pos += 1

    Sort(arrLen, pos, minval)
    If pos >= arrLen Then
        IsSorted = True
    End If

    If IsSorted = True Then
        Exit Sub
    End If

End Sub

结束班

构造函数中的所有内容都在基类“DefaultSort”中设置,但Sort()除外。使用属性

设置数组
Public Overridable Property Arr() As Integer()

    Get
        Return _array
    End Get

    Set(ByVal value() As Integer)
        _array = value
    End Set

End Property
Private _array() As Integer

据我所知,一切都应该有效。它适用于10个元素的数组和100个元素的数组。

任何想法,卡住!!! :)

1 个答案:

答案 0 :(得分:2)

不要为此使用递归。请改用迭代解决方案

<强>更新 如果您查看StackOverflowExectpion的文档,则说明

  

当抛出异常时   执行堆栈溢出因为它   包含太多嵌套方法调用。

编写此类的方式使用递归方法调用访问每个元素。例如第一个元素调用第二个元素的sort,它为第三个元素调用sort,依此类推。这意味着递归深度等于成员数(我可能会偏离+/- 1)。

这意味着如果你有足够的元素,你将获得StackOverflow。

我复制了你的代码然后运行它,下面是截断的调用堆栈,它表示在每个pos递归调用sort,并且当有超过8,314个元素时它会死掉。

MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8314, Integer minval = 1) Line 67 + 0xb bytes  Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8314, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8313, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8312, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8311, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8310, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8309, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8308, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8307, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8306, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8305, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8304, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8303, Integer minval = 1) Line 75 + 0x14 bytes Basic
MyApp.exe!MyApp.SelectionSort.Sort(Integer arrLen = 10000, Integer pos = 8302, Integer minval = 1) Line 75 + 0x14 bytes Basic