There is an IEEE article on doing a circular binary search
http://ieeexplore.ieee.org
Main Topics
Browse All Topicshi,
heard this was a google interview question and had need to use it myself.
if i have a circular, sorted list of n values x(0), x(1), x(2), ..... x(n-1) , and given a value u, what is the index that satisfies
x(i) <= u < x(i+1)
iterative searching seems wasteful. i'd use a binary search on this, but how do you handle the circular nature of it? what do you do at the crossovers? are there any c++ algorithms out there that do this?
what would YOU do?
lou
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
There is an IEEE article on doing a circular binary search
http://ieeexplore.ieee.org
spprivate that article is not a simple binary search lol ... it looks like some link love from plugging keywords into a goodle search.
References? for which? is it an array or is it a linked list?
For an array do you really need one? its a simple arithmetic mapping to take the circular array and present it as 0-n. If you have a circular implementation just look at an index translation which is probably there already.
For a skip list http://en.wikipedia.org/wi
Greg
hi,
it's a list, but not a linked list ... i.e. an array, but a circular and sorted array.
without having to get as complicated as a skip list, how would you do a simple binary search on this and then handle the problem of not knowing where the end points are?
let's say the list (array) is
{ 7,8,9,10,11,12,1,2,3,4,5,6
now i want to find out where 3 belongs. if i split the array in half, half 1 will be sorted, but half 2 will contain a jump. is there an efficient way to handle that?
9 0 1 2 3 4 5 6 7 8
head = 1
tail = 0
3 4 5 6 7 8 9 0 1 2
head = 7
tail = 6
head and tail are both on the same side ... that side is offset
0 1 2 3 4 5 6 7 8 9
head = 0
tail = 9
both sides are sorted
If you later end up with neither head or tail in your segment you know that it is sorted continue as a normal binary search.
We know that we can do a normal binary search if we are on the sorted side. If we are not on the sorted side then we have to realize where our offset is
head->length = sorted
tail->start = sorted
From here we can keep with a binary search and just take the head/tail as a split point to figure out which one to search
we know that
start -> tail is sorted
and that head->length is sorted
so do some quick checks to see which of those ranges we fall into pick the one that we fall into and do a normal binary search ... If you only have one then pick that range (the rest is space you don't care about)
Hope this helps you in terms of more detail, I saw you commented on the question that you wanted more.
Greg
Business Accounts
Answer for Membership
by: gregoryyoungPosted on 2009-04-14 at 10:53:12ID: 24140785
if its in an array just do an offset binary search, its just a matter of adjusting the index appropriately for a binary search.
take a look at how to calculate the length of a circular in an array, it has pretty much everything you need to do offsetitng.
if its in say a linked list ... google (pun intended) what a skip list is ...