Quote:
|
When I entered the 60 with a bin search should't it produce a 2
|
No, because the value returned is the INDEX of the SORTED array.
You will need to backup the unsorted array if you want to know its index from the unsorted array.
Quote:
|
bin search for 80 shouldnt that produce a 1
|
No. It should return the index of the SORTED array.
Quote:
|
I always thought the bin search cut the array in half checking it, and if it wasnt found cutting it again, until it was found?
|
Yes, but not relevant. Usually you return the index of the sorted array, or the unsorted array. It doesn't make sense to return the index of the "cut-array" IF found, since the index is always 0 ( by design for binary search).
A last note:
Your output isn't correct as you posted it above. Note that the algorithms return the index of arrays. Arrays start at index "0" by default. The correct output should be:
VALUE LIN BIN
60 1 4
80 5 6
For example, 60 is at PHYSICAL location "2". But since arrays start at index 0, the correct output is 1.
Same goes for 80.
So
LOGICAL = PHYSICAL -1 by default.
I notice you added the number 101 to your original array. In that case make sure it can hold 11 numbers (10 in the original code remember?).
Solve it for good by typing this in main():
int theArray []={ 40,60,30,70,90,80,5,25,95,100, 101 };