DEV Community

Discussion on: Binary Search In Javascript

Collapse
 
bernardbaker profile image
Bernard Baker

It would great to see the binary search on a unordered list.

Would you sort the list first? How would you approach that situation?

Collapse
 
polmonroig profile image
Pol Monroig Company • Edited

If it is unordered the best way to go is linear search, in case you need to search an LOT of times then and only then I would consider sorting it. Since sorting with comparison methods has at least O(nlogn). Altought i'm ignoring counting methods that can be done linearly

Collapse
 
ogzhanolguncu profile image
Oğuzhan Olguncu

I said I would sort it regarding his question. Since, he asked "how would you sort list first", but you are right going with linear search makes sense if it's unordered.

Collapse
 
ogzhanolguncu profile image
Oğuzhan Olguncu

Sadly, can't perform binary search on an unordered list. I would first consider using something like quick sort or selection sort depending on the situation.