• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Minimum Function

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats how the teacher wants it, I know how to do it using a for loop, it needs to be done recursively.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic