Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Binary search of float array

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doing an early exercise out of the Java Examples in a Nutshell book and they are asking for 'an efficient search algorithm to find the desired position' of two floats in a sorted array that bound an int. My try is below:



This is working for the example but I would like to know if it is considered 'efficient' or even good?
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to find the index of the element that matches searchnum? What do you want to return if searchnum doesn't exist in the array? It looks to me that with line 6 and 7, if searchnum falls between the elements at indexes n and n+1, you return n. Is that what you want? What happens if the searchnum you pass in is less than the element at index 0, or greater then the element at index length-1?

It's anticipating and testing for exceptional cases like these that makes your programs resilient, plus it's sort of fun too! It's like a brain teaser or other puzzle, and if you don't love puzzles, you probably won't enjoy programing!
 
Scott Halepaska
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:Are you trying to find the index of the element that matches searchnum? What do you want to return if searchnum doesn't exist in the array? It looks to me that with line 6 and 7, if searchnum falls between the elements at indexes n and n+1, you return n. Is that what you want? What happens if the searchnum you pass in is less than the element at index 0, or greater then the element at index length-1?



The exercise was to produce a random float[] and then print the floats that border a user inputted int. It works as expected and it could surely be hardened as you mention for better tests. Really just wondering if it is decent implementation. Is it feasible that an int would ever equal a random float? Might have to deal with that also. Thanks for your time.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic