jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Best way to search for an element in integer array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best way to search for an element in integer array" Watch "Best way to search for an element in integer array" New topic
Author

Best way to search for an element in integer array

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Apr 15, 2006
Posts: 418
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

Joined: Jul 14, 2010
Posts: 754

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.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

"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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to search for an element in integer array