• 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

Searching an array

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic