This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching an array

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i have a variable int x. Does anyone know how to SEARCH AN UNSORTED ARRAY to see whether this item is in it.

Cheers
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

there are many ways to search an array/list of elements (numbers for example).

I'd like to begin with a small note on searching:
If your supposed to search for a number in the list, it might be a good idea
to build a datastructure from the array/list of elements, and then search
for the number in that datastructure.

Ok, so how do you find x?

I'll show you in pseudo code (which might give you a pointer)


Hope it helped.. feel free to reply back if your still having problems.

/Svend Rost
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

SEARCH AN UNSORTED ARRAY

Yes, you do this with a linear search, as opposed to performing a binary search in the case where the array is sorted. Doesn't your text book already tell you all of this? It is very fundamental.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?

Thanks
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Morris,

may i remind u this forum page is for beginners. Maybe you should try the intermediate forum page if u find these questions too fundamental for your liking
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Bluesman:
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?

Thanks


Staying in pseudo code, because it better learning to code it yourself...

Basically, that is what the conditionals are for. When you need to actually use it, you'll do something like this:



Henry
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Bluesman:
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?

Thanks


Well, it depends on what you want to do. Can you describe the problem you are trying to solve? I assume this is for a homework assignment. Is the homework to implement a search algorithm? Or are you supposed to actually use it for something bigger? It will help a lot if you provide some more details about what you are trying to accomplish here.

Layne
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

Originally posted by Sam Bluesman:
Does anyone know how to SEARCH AN UNSORTED ARRAY to see whether this item is in it.


Avoid using uppercase, it appears as if you're shouting. Use bold or italics to catch attention instead.

Originally posted by Sam Bluesman:
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?


Well, in the first place, why are you checking to see if X is in the array? There ought to be a reason for it. With that reason, you should know what you need to do if the value is in it right? It could be simply just telling that the value is in the array, or it could be part of a bigger problem.

Originally posted by Sam Bluesman:
may i remind u this forum page is for beginners. Maybe you should try the intermediate forum page if u find these questions too fundamental for your liking


I suppose Tony was trying to help you to perform self-help here. I don't think he meant to be offensive. Honestly speaking, any decent computer science textbooks would certainly tells you what search to use in an unsorted or sorted array, and they may contain the pseudo-codes as well. Maybe he was trying to get you to read the textbook better.

If you'd converted the pseudo-codes to Java codes & encountered problem compiling or some bugs here & there, feel free to post it here for help.

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Bluesman:
Tony Morris,

may i remind u this forum page is for beginners. Maybe you should try the intermediate forum page if u find these questions too fundamental for your liking


I didn't say whether or not I liked anything, but since you asked I like it when those who are learning can demonstrate initiative in respect to the topic at hand. I am merely pointing you in the right direction - you do have a text book don't you?

The one I used for my education is obsolete (many will disagree here) - I'm thinking of putting it on ebay for two loaves of bread and a fish, but the examples are in C.

<spam>
Want it? I'll mail it anywhere in Australia for AU$5.
http://www.amazon.com/gp/product/0201498405/103-0263553-5951812?v=glance&n=283155
</spam>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic