Web Development

General discussion


C++ Linear Search / Binary Search???

By Mr_joey ·
I am new to programming and I am learning about the Linear and Binary Search methods built into the C++ Code (using Visual C++ 6.0). My question is which method is preferred by most programmers and why. Which one is most efficient, and which one is easier to program?


-Joe (joe.velderman@trnty.edu)

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

Jay on bin/linear search

by jaysonabas In reply to C++ Linear Search / Binar ...

Well Joe, that's a great question, and I have an answer for you.
The linear search is the slowest possible search, it's like looking through a website, word by word starting at the beginning.
That's not a bad search by any means for smaller searchs, and technically, all you need to do is loop sequentially until you find what you need.

However, if you were looking up a phone number in a phone book, it would be a different story.

We've talked about a Alphabetic Algorithms (Phone Book), Linear Search Algorithm, now a Binary Search Algorithm.

The binary search is like the alphabetic search in that:

1) The Numbers or Letters you're searching through have some kind of prearrangement --

in other words "1,3,7,14,67" are arranged lowest to highest

2) You can then skip some of the entries (just like you can skip "A's" when looking for "Jack Barns" in the phone book.

Now, you probably use an Alphebetic way to look for a Phone Number, but I'll tell you the BINARY way.

Binary, of course means 2. Two what, you may ask?

Pick up a Phone book and look up a person named "Tom Swift"(fictional), but follow THIS procedure:

1. Open the phone book to the dead center page

(if you did this you'd probably see names binginning with "J's" or something.

2. Keep your finger at the "J" position and ask yourself "Are S's positioned in the Phone Book before or after the J's"...(After of course...2 choices, imagine that!)

3. Now choose the "Middle" between where your finger is now and the "End" of the book.

4. Now REPEAT this until you find it.

It's really fast, but you need to sort the information first to use it - so take a look at bubble sort algorithm, gots to go, keep banging away

Related Discussions

Related Forums