tags:

views:

104

answers:

3

Possible Duplicate:
Searching a number in a rotated sorted Array

Say the original list is 1 2 3 4 5 6 7 8 9 10

and u shift it so that it becomes

5 6 7 8 9 10 1 2 3 4

so say i want to check if 7 is in the array. how would i do this efficiently.

A: 

Unshift the list (whether physically, or just via lookup logic), then binary search?

Amber
+2  A: 

Use a variation of binary search to find the point in the array where an element is less than the the prior element; this identifies the shift point. Now break the array in half at that point and do a binary search in whichever half contains the element you're searching for.

Mark Ransom
How to find such a shift point in the array using binary search?
Michał Trybus
@Michał Trybus, it's hard to explain in just a few words. You're searching for the lowest value in the array that's less than array[0], which requires changing the testing condition and the adjustment of the range after each test.
Mark Ransom
Thanks, this makes sense.
Michał Trybus
+3  A: 

Use ternary search. It works similarly to binary search (and also in logarithmic time), but lets you find the element when the sequence is shaped like a wedge (/) or a vee (\/).

finrod