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

  • Home
  • SEARCH
  • 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 6029335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:52:16+00:00 2026-05-23T04:52:16+00:00

Trying to sort an array by writing my own sort method using recursion (Pine’s

  • 0

Trying to sort an array by writing my own sort method using recursion (Pine’s book). Saw some other examples on stackoverflow, but my code looks different from them. Two things I don’t understand so far:

  1. What is a wrapper method, and why do I need one? (I put on in the code, I think).
  2. How to fix the “stack level too deep” error.

EDIT: New code updated, working but not correct.

Here’s what I have so far:

def word_sorter unsorted, sorted
  if unsorted[1] == nil
    sorted.push unsorted[0]
    words_put(sorted)
  elsif unsorted[0] <= unsorted[1]
    sorted.push unsorted[0]
    unsorted.shift
    word_sorter(unsorted, sorted)
  else
    unsorted.push unsorted[0]
    unsorted.shift
    word_sorter(unsorted, sorted)
  end
end

def words_put sorted
  puts 'these are your words now organized.'
  sorted.compact!
  puts sorted.join(', ')
  Process.exit
end

unsorted = Array.new
sorted = Array.new
puts 'list as many words as you want. I will sort them... I think'
while unsorted.last != ''
  unsorted.push gets.chomp
  if unsorted.last == ''
    unsorted.pop
    word_sorter(unsorted, sorted)
  end
end

Thanks!

  • 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-23T04:52:17+00:00Added an answer on May 23, 2026 at 4:52 am

    You should first try your code by some simple examples. E.g. use the list [3,2,1] as input.

    In the first call it will match the 3>=2 condition. Thus now sorted=[2].

    There are two issues with this one already.

    1. 2 isn’t the first entry in the sorted list. There must be an issue with your algorithm being not able to sort any input.
    2. The array unsorted isn’t changed at all and thus it will loop with this one forever, yielding sorted=[2,2,2,2,2.....].
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort my array while using NSPredicate.. I have read other
I am trying to sort an array using multithreading in java and note its
I'm trying to sort an array in PHP using a custom function, but it's
I am trying to build a function in C/C++ to sort an array and
I am trying to sort a list using delegates but I am getting a
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I am trying to write a JTextPane which supports some sort of coloring: as
Trying to sort this array of objects according to (1) depth and (2) weight,
Trying to sort the array below by memnum in ascending order, and I'm a
I am trying to sort an array that contains numbers that range in substantial

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.