Hi I am trying to implement Selection Sort in vb.net using recursion. Problem is that I keep getting stackoverflow if the array I want to sort is 1000 elements. I can't find the problem. I don't know much about stack overflow or how to check it. From my research the problem could be infinite recursion but I check for that and exit the sub.Here is the main code:
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
End Class
Everything inside the constructor is set in the base class "DefaultSort", except for the Sort(). The array is set using properties:
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
Everything should work as far as I know. It works on an array of 10 elements and an array of 100 elements.
Any ideas, stuck!!! :)