Results 1 to 5 of 5
  1. #1
    andy11 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Recursion with lists

    Hi guys , can u help me? im suppose to write a boolean method

    public static boolean isSorted(List l)

    that returns true when a list is sorted....can u help me ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Recursion with lists

    Quote Originally Posted by andy11 View Post
    Hi guys , can u help me? im suppose to write a boolean method

    public static boolean isSorted(List l)

    that returns true when a list is sorted....can u help me ?
    You first please.

    Please show us what you have so far as well as how it isn't working.

  3. #3
    andy11 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Recursion with lists

    public static boolean isSorted(List l) {
    if (l.isEmpty())
    {
    return true;
    }
    else if(l.tail().isEmpty())
    {
    return true;
    }
    else if (now here is my problem i want to check the head of the list with the next element of the tail and check if l.head() is lower than the first element of the tail and so on,here is my problem i dont understand how to do it recursively

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Recursion with lists

    Try to say it in words: if the list is empty or the list's tail is empty, the list is sorted, otherwise if the list's head isn't larger than than the head of the tail of the list and the list's tail is sorted the the entire list is sorted.

    The phrase in italics is the recursion step.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    andy11 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Want to know something about lists.
    By orushw in forum New To Java
    Replies: 2
    Last Post: 05-08-2012, 10:41 PM
  2. A little trouble with Lists....
    By kovalensue in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 10:56 PM
  3. Help with lists
    By datreta in forum New To Java
    Replies: 6
    Last Post: 10-29-2010, 12:33 PM
  4. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  5. Tudu Lists 2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-11-2007, 04:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •