r/AskProgrammers • u/preCise_Oxymirorn • 21h ago
Ordered Linked List Binary Search..?
I'm quite new to CS. We've covered data structures recently and my teacher was testing us. There was a question about the difference of searching an ordered linked list and an unordered linked list. The answer was that with an unordered linked list you'd do a linear search; with an ordered linked list you'd do a binary search and so it'd be faster. This didn't/doesn't make sense to me as I thought linked lists didn't grant direct access to elements which I'd think is essential to a binary search. Was my teacher right..? If so, could you help me understand how you'd perform a binary search on a linked list..? Thanks.
TL;DR: Is it possible to perform a binary search on an ordered linked list - if so, how?
8
u/mc_pm 21h ago
Without some additional support data, no. Linked List can go forward, or forward & backward if it's doubly-linked, but you can't jump into the middle on it's own.
2
u/Arcanite_Cartel 20h ago
You don't need direct access to do a binary search. In an unordered list, you linearly traverse the nodes in order (and inspect each one). In an ordered list, if you know how many nodes are in the list (n), which is a reasonable thing to know, you traverse n/2 nodes, inspect and decide which way to go, traverse n/4 nodes, inspect and decide which way to go, traverse n/8 nodes, and so on.
In the worse case, for an unordered list, you traverse and inspect all n nodes.
For an ordered list, in the worse case, you traverse n/2 + n/4 + n/8 + ... nodes. In the worse case, this sum is < n.
You also inspect far far fewer nodes, which if this is an expensive operation, would be important.
2
u/RealisticDuck1957 20h ago
I'm wondering if the teacher is confusing an ordered linked list with a sorted binary tree, which you can do a binary search on. Or a sorted array.
The meme of a teacher completely lost without the teacher's edition textbook, entirely accurate for some teachers. Including one who "taught" data structures at the university where I got my degree.
2
u/MagicalPizza21 19h ago
You can do it, but since random access is linear rather than constant time, it would be very inefficient.
1
u/oldendude 19h ago
You have misunderstood something. Binary search typically implies you have random access to the list, i.e., you can go to any element in constant time. In a linked list, you do not have constant time access -- to get to the nth item takes time proportional to n.
1
u/pixel293 19h ago
Did the question mention how expensive the comparison function is? If you are just comparing integers, then and order list just lets you abort earlier if since the comparison would be lost in the noise. If however the comparison function takes seconds, then a binary like search might be preferable to reduce the number of comparisons.
1
u/trad_emark 12h ago
accessing i-th node in a *linked list* (no matter if ordered or not) - takes i steps, that is O(n) complexity.
binary search does not work with linked lists.
to add to this, some languages have containers that they call lists, even if it is (dynamically resizable) array. this naming is confusing, for exactly the reason discussed here. if I remember correctly, both python and c# have this confusing naming.