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
Moosey knows best
Svend Rost
Ranch Hand
Joined: Oct 23, 2002
Posts: 904
posted
0
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
Joined: Sep 24, 2003
Posts: 1608
posted
0
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.
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
Joined: Nov 21, 2004
Posts: 188
posted
0
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
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:
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.
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.
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.