Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Sorting and Searching: Most Efficient Code

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sorting and Searching: Most Efficient Code

    I am trying to create a method to search a sorted array for a value in the most efficient way possible. My teacher doesn't want us to use binary search yet. This is the code I have so far, is there any way to make it more efficient?

    public int sortedSearch(int value)
     {
      for(int x=0;x<a.length; x++)
      if(a[x]<=value)
      {
       if(a[x]==value)
        return x;
      }
      return -1;
     }
     
     private int[] a;

    Thank you.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Sorting and Searching: Most Efficient Code

    Make the inner if statement the first thing the code does in the loop. Why do anything else when you have found the number?

    Stop searching as soon as the value in the array is larger than the target number. Currently you continue searching all the way to the end.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting and Searching: Most Efficient Code

    public int sortedSearch(int value)
     {
      for(int x=0;x<a.length; x++)
      {
      if(a[x]==value)
        return x;
      }
      return -1;
     }

    Is this better? I don't think it is efficient still.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Sorting and Searching: Most Efficient Code

    That has addressed my first point but not my second point.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting and Searching: Most Efficient Code

    public int sortedSearch(int value)
    {
    int x=0;
    while(x<a.length)
    {
    if(a[x]==value)
    {
    return x;
    }
    else
    x++;
    }
    return -1;
    }
    private int[] a;

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Sorting and Searching: Most Efficient Code

    That still does not address my second point.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting and Searching: Most Efficient Code

    I don't get what you mean

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Sorting and Searching: Most Efficient Code

    Imagine you have an array with 1000 numbers in it: 1, 3, 4, 5, etc. Then you attempt to search for 2. When you get to 3 (the second element) you know that 2 is not in the array. However your code continues searching another 998 times. Not very efficient at all. What you need to do is have the loop stop as soon as it knows the target number is not in the array and return -1.
    Improving the world one idiot at a time!

  9. The Following User Says Thank You to Junky For This Useful Post:

    nicsa (November 29th, 2011)

Similar Threads

  1. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 09:07 AM
  2. Searching network for server
    By Jonathan_C in forum Java Theory & Questions
    Replies: 2
    Last Post: November 14th, 2010, 11:23 AM
  3. searching a string
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 01:31 AM
  4. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM
  5. Searching Data
    By kalees in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 2nd, 2009, 03:23 AM

Tags for this Thread