Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3244416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:32:55+00:00 2026-05-17T18:32:55+00:00

Hi I am trying to implement Selection Sort in vb.net using recursion. Problem is

  • 0

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!!! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:32:55+00:00Added an answer on May 17, 2026 at 6:32 pm

    Don’t use a recursion for this. Use an iterative solution instead

    Update:
    If you look at doc for StackOverflowExectpion it says

    The exception that is thrown when the
    execution stack overflows because it
    contains too many nested method calls.

    The way this sort is written each element is visited with a recursive method call. e.g. the first element calls sort for the second element which calls sort for the third element and so on. This means that the recursion depth is equal to the number of member (I might be off by +/- 1).

    This means that if you have enough elements you’ll get a StackOverflow.

    I copied your code and ran it and below is the truncated Call Stack thats shows that the sort gets called on each pos recursively and that it dies when there’s more than 8,314 elements.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.