QuickSort中的StackOverflowException

时间:2017-07-11 18:48:01

标签: vb.net sorting recursion stack-overflow

我需要使用快速排序来订购一些非常大的列表(数千个项目)。但是,当我尝试这个时,我得到异常System.StackOverflowException。

从一些快速的谷歌搜索,我知道这要么是有一个非常大的列表,但是我已经通过使用小列表上的函数排除了这种可能性)或者有一个无限递归调用的子程序。虽然下面的代码位确实使用了递归,当我删除一个子程序Swap()的调用时,异常停止发生,尽管Swap()实际上没有调用任何其他子程序。

有人能发现这个代码的任何问题吗?这是我第一次使用递归。

#Region "QuickSort"
    'Subroutine for QuickSort, called upon recursively until list is sorted'
    Private Sub QuickSort(ByRef list(,) As Integer, ByVal min As Integer, ByVal max As Integer) 'min is index of first term, max is index of last term'
        If min < max Then 'Checks if list is sorted'
            Dim pivotLoc = Partition(list, min, max) 'Gets the next pivot position'
            QuickSort(list, min, pivotLoc) 'Sorts the two new sublists'
            QuickSort(list, pivotLoc + 1, max)
        End If
    End Sub

    Private Function Partition(ByRef list(,) As Integer, ByVal min As Integer, ByVal max As Integer) As Integer
        Dim pivot = list(min, 0) 'Initially sets the pivot to be the minimum value in the list'
        Dim leftWall = min

        For i As Integer = min + 1 To max 'For each item in sublist'
            If list(i, 0) < pivot Then 'If current item is less than the pivot swap it onto other side of pivot'
                Swap(list, i, leftWall)
                leftWall += 1 'Increment leftWall'
            End If
        Next

        Swap(list, min, leftWall) 'When this line exists System.StackOverflowException occurs'
        Return leftWall
    End Function

    'Subroutine that swaps values in list around using temporary storage variables'
    Private Sub Swap(ByRef list(,) As Integer, ByVal x As Integer, ByVal y As Integer)
        Dim tempVal = list(x, 0)
        Dim tempIndex = list(x, 1)

        list(x, 0) = list(y, 0)
        list(x, 1) = list(y, 1)

        list(y, 0) = tempVal
        list(y, 1) = tempIndex
    End Sub
#End Region

谢谢,Alex。

编辑:如果它对任何人有帮助,这里是伪造的代码,我将其基于:here

1 个答案:

答案 0 :(得分:3)

这是可行的解决方案:

#Region "QuickSort"
    'Subroutine for QuickSort, called upon recursively until list is sorted'
    Private Sub QuickSort(ByRef list(,) As Integer, ByVal min As Integer, ByVal max As Integer) 'min is index of first term, max is index of last term'
        If min < max Then 'Checks if list is sorted'
            Dim pivotLoc = Partition(list, min, max) 'Gets the next pivot position'
            QuickSort(list, min, pivotLoc) 'Sorts the two new sublists'
            QuickSort(list, pivotLoc + 1, max)
        End If
    End Sub

    Private Function Partition(ByRef list(,) As Integer, ByVal min As Integer, ByVal max As Integer) As Integer
        Dim pivot = list(min, 0) 'Initially sets the pivot to be the minimum value in the list'
        Dim pivotIndex = list(min, 1)
        Dim leftWall = min

        For i As Integer = min + 1 To max 'For each item in sublist'
            If list(i, 0) < pivot Then 'If current item is less than the pivot swap it onto other side of pivot'
                Swap(list, i, leftWall)
                leftWall += 1 'Increment leftWall'
            End If
        Next

        'Swap(list, min, leftWall) 'When this line exists System.StackOverflowException occurs'

        'Instead do this'
        list(leftWall, 0) = pivot
        list(leftWall, 1) = pivotIndex

        Return leftWall
    End Function

    'Subroutine that swaps values in list around using temporary storage variables'
    Private Sub Swap(ByRef list(,) As Integer, ByVal x As Integer, ByVal y As Integer)
        Dim tempVal = list(x, 0)
        Dim tempIndex = list(x, 1)

        list(x, 0) = list(y, 0)
        list(x, 1) = list(y, 1)

        list(y, 0) = tempVal
        list(y, 1) = tempIndex
    End Sub
#End Region