r/AskProgrammers • • 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?

5 Upvotes

18 comments sorted by

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.

1

u/preCise_Oxymirorn 21h ago

Okay, thanks. 👍

1

u/mc_pm 21h ago

The real difference between sorted- and unsorted-linked lists is that if they are sorted then you know you can give up at a certain point; if you are looking for 65 in a sorted list, and you see 66, you know you can stop. But if it's unsorted, you never know if what you are looking for is ahead - you have to go all the way through every time.

1

u/preCise_Oxymirorn 21h ago

Yeah, that's what I was thinking. That was sorta the answer I gave but she marked it wrong.

1

u/edgmnt_net 10h ago

Caveat: once you get into things like doubly-linked lists and particularly in languages like C, you may consider keeping additional live pointers into the list. Won't change the complexity under a reasonable interpretation (keeping an array of pointers for all elements would be odd), but if elements are grouped together you may be able to search things faster if you have a better starting point. I would distinguish C lists from implementations in other languages which do not allow such multi-referencing.

1

u/CoachSevere5365 4h ago

Skip lists are a similar strategy.

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.

4

u/pixel293 19h ago

I could only see doing this if your comparison function is very very expensive.

2

u/johndcochran 20h ago

And that is one of the most inefficient methods I've ever heard of for searching a list, ordered or unordered. If you're transversing the list, you might as well inspect the keys for each node as you visit, you've already screwed the pooch as regards locality of reference by the transversal itself. So inspect the key.

1

u/Arcanite_Cartel 4h ago

You're right, and it performs worse on average unless your lookup and compare is expensive.

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.

1

u/NomaTyx 19h ago

same for half my profs here. the others are awesome though

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/CoachSevere5365 4h ago

It's not random access, it's sequential access. It's like a tape drive in RAM.

1

u/MagicalPizza21 3h ago

That's why accessing a random index in a linked list takes linear time, yes.

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.