wood burning stoves 2.0
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Minimum Function" Watch "Minimum Function" New topic

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

Joined: Mar 22, 2005
Posts: 42965
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 ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Minimum Function
It's not a secret anymore!