Saturday, 15 May 2010

Java: Recursively Finding the minimum element in a list -


I'll call it homework. I'm just looking for some points I'm ejecting my brain with this one, and I'm not getting it for my life. We are asked to find the minimum element in a list. I know that I need a novel here, but after that I'm not sure. Any hints would be great thanks.

  / ** Find the minimum element in the list. * * @ Ultimate * integer list * * * return the minimum element in the list / public static end minute (list & lt; integer & gt; t) {if (t.size () == 1) {return t.get ( 0); } And {list & lt; Integer & gt; U = T. SULIST (1, TCISE); In the most common sense, recursion is a concept based on breaking a job, and    

  1. Breakdown of work How are you making each step "simple" ?
  2. Recursive call. At some point your function should call itself, but with less "work".
  3. What is the case of the base case one (usually trivial) that will stop the recursive process?

    In your case, you are trying to create a function min which is a list. You are right in thinking that you can reduce your work by making a list short by one time (first thing can be taken out of the element). As others have mentioned, this idea is to check the first element (which you have just drawn) against the "rest of the list". Anyway, where the leap of faith comes. At this point, you can assume that your minute function will work on the novel, and just call a function on the novel (recursive call). Now you have to make sure all your calls will be back (i.e., make sure that it will not always buy again). This is where your base case comes. If your list size is 1, then only one element is the smallest in the list. There is no need to call again minutes , just return it (the part you already did in your original post).

No comments:

Post a Comment