• 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

Best way to search for an element in integer array

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an integer array. Elements in this array are not in any particular order. What is the best and most efficient way to find an element in this array.

Thanks in Advance.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are only going to have to do it once? Start at the beginning and go the array until you find the element, or don't find it.

If you are going to have to do it many, many times? Sort the array and use a binary search.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to search only once. But, i was just thinking if there can be any better approach than using linear search.May be if we can use some data structure to optimise the performance.
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could get it from its index "intArray[3]". But if your code do not have a way to know the index, I believe you will have to do a linear search.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Better" in terms of...what? Speed? Memory use? Code complexity?

My point is that unless and until you define what's important, then any discussion is meaningless.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Kumar Bindal wrote:I need to search only once. But, i was just thinking if there can be any better approach than using linear search.May be if we can use some data structure to optimise the performance.



Absolutely, you could use some data structure. But that wasn't what you said. You said "I have an integer array. Elements in this array are not in any particular order." It's certainly true that if you had used some other data structure -- even an integer array with the elements sorted -- you could do the search faster.

Of course it might take you longer to produce that other data structure than it would take to produce the plain old unsorted integer array. You might spend more additional time or memory or whatever you meant by "performance" than you would save in searching that other data structure.

But these are just generalities. All one can really say without any particular requirements is "It depends".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic