This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to search for an element in integer array

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic