I am trying to create a merge sort in to visual basic and keep getting the stack overflow error. I have no idea of how to fix this error and would like some help. Here is my code:
Sub Main()
Dim items() As String = {"Florida", "California", "Alabama", "Delaware", "Georgia"}
Dim items2() As String = {"Florida", "California", "Alabama", "Delaware", "Georgia"}
Mergesort(items, items2, 0, UBound(items))
End Sub
Sub Mergesort(ByVal items() As String, ByVal items2() As String, ByVal low As Integer, ByVal high As Integer)
If low = high Then
Return
End If
error on this line:
Dim mid As Integer = (low + high) / 2
Mergesort(items, items2, low, mid)
Mergesort(items, items2, mid + 1, high)
Merge(items, items2, low, mid, high)
End Sub
'''
here is the error:
*System.StackOverflowException HResult=0x800703E9 Message=Exception of type'System.StackOverflowException' was thrown.*
I am trying to get the output to be the ordered list to be in alphabetical order:
["Alabama","California","Delaware","Florida","Georgia"}
There is a built in method to do what your want.
Private Sub OPCode2()
Dim items() As String = {"Florida", "California", "Alabama", "Delaware", "Georgia"}
Array.Sort(items)
For Each i In items
Debug.Print(i)
Next
End Sub
Immediate Window result
Alabama
California
Delaware
Florida
Georgia
Dim mid As Integer = (low + high) / 2
Changed this line of code to:
dim mid as integer
mid =(low + high) /2
This seemed to fix the error
User contributions licensed under CC BY-SA 3.0