It is possible to apply the Binary Search to a chained list, however, you will lose the main reason to use the Binary Search, the factor it has complexity O(log n).
The fact is that binary searches applied in arrays are efficient, because the elements of an array are allocated in memory in a contiguous manner, which makes access to any element a trivial operation, O(1).
On the other hand, in a chained list there is no guarantee that the elements will be allocated in a contiguous manner and, even if they were, there is no way to access them in time O(1). That way, to find the middle of the list, you need to unravel an algorithm that will have to visit us until you find the middle. This operation already slows down the algorithm. The worst case of a binary search in a chained list is O(n log n).
Because of this, sequential searches on chained lists tend to be faster, since O(n) < O(n log n). See update below for cases where it might be worth using binary search in a chained list.
About the algorithm, this one looks interesting: http://www.algo-faq.com/Linked-List/Find-the-middle-node-of-a-linked-list.php
The challenge, compared to arrays, is to determine the middle of the list. For this, the above algorithm uses a different approach that does not require knowing the size of the list previously.
This same algorithm is described in this article: http://www.ijcsit.com/docs/Volume%205/vol5issue02/ijcsit20140502215.pdf
Since this is an exercise, I would go the way mentioned above.
Updating
There are cases where it can make sense to use binary search in a chained list, I explain.
Suppose the task of making the comparison is costly. In this case, the binary search algorithm may make sense, as O(log n) comparisons are made. In part O(n) no comparison is made, but only the list is searched to find the medium.
On the other hand, if the algorithm is sequential search then, for each item, the comparison is performed. What can become something more costly compared to the binary search scenario.
The list is ordered, I import it from a text file, but the amount of elements "I do not know beforehand".
– Marcelo de Sousa
And you can count the elements ?
– Mayllon Baumer
I did not understand why this question was classified as too wide.
– cantoni
What do they mean "EP",
p
andq->prox
, within the problem?– Gabe
@Marcelodesousa, I think it’s worth it you give an improved in your question. You’ve already had 10 votes with her like this. If you do this, your question has a chance to be reopened and gain more community attention, with more answers and discussion on this interesting topic.
– cantoni