This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Minimum Function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Minimum Function" Watch "Minimum Function" New topic
Author

Minimum Function

Steven Alvarez
Ranch Hand

Joined: Nov 01, 2006
Posts: 66
I need help writing a recursive minimum function. For the minimum function, lets say you have an array like 5,12,15,20. You would split it in half, so on one side you get 5 and 12 and the other 15 and 20. Therefore you compare 5 and 12 and 5 wins, then you compare 15 and 20 and 15 wins. Then you compare 5 and 15 and 5 wins, making that the minimum.

My brother sent me something but it looks way to complicated. Can someone make the same function but with less lines? Thanks for any help!!!

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Why are you splitting the array instead of just doing a linear search for the minimum? Thats a lot less complicated and just as efficient. A non-recursive minimum function is as easy as:



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Steven Alvarez
Ranch Hand

Joined: Nov 01, 2006
Posts: 66
Thats how the teacher wants it, I know how to do it using a for loop, it needs to be done recursively.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
The general approach might be: The minimum over all N elements is the minimum of the first element and the minimum of the N-1 remaining elements. That reduces the problem to finding the minimum of an actual number and the result of a recursive call.

But nobody here will just give you some code - this is an assignment that you should be doing. How are you going to learn if you hand in someone else's solution?
[ May 07, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Minimum Function
 
Similar Threads
TicTacToe Game..
The fizz buzz coding challenge
Help Please
help please
multithreading not working correctly