• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding smallest int in array using recursion  RSS feed

 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jave'ers,

I am currently working on a program that is to be able to calculate the smallest int from an int[] array, but with a few conditions: that my method is static, that I do not use static variables, that I do not use any loops within the method, & that I use recursion in the method in order to find the smallest number. I have been dreaded with this for 3 days now and am going a little bit mad tbh. I keep trying, deleting, trying and deleting but cannot solve this. I am feeling pretty incompetent by now. I am wondering if anyone can lead me i the right direction on how to do this. I just need a different point of view because mine's is clearly not the right one. Thanks in advance! Below is the code:

 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got a lot of code there but it isn't going to give you the right answer.

Basically you want to be comparing the first element in the range (start-index to end-index) to the minimum of the rest of the elements in the range. That's how the recursive part comes in. I'll leave you to work out how to stop the recursion, it's similar to some things you already have in your post.
 
Carey Brown
Saloon Keeper
Posts: 3308
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There's no need to pass in the endIndex it can be determined by the length of the numbers array. Then, instead of startIndex it would be currentIndex which would be set to zero in the first call and incremented by one for each recursive call.

I Think that you are overthinking the plumbing. you should be able to implement the method with about 5 lines of code in the body.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!