Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:24:21+00:00 2026-05-28T13:24:21+00:00

I am not sure why List as a general data structure should have binary

  • 0

I am not sure why List as a general data structure should have binary search algorithm given the list is sorted. Doesn’t the get method which accepts the index traverse the List sequentially, at least for List‘s subtype LinkedList? If so, I do not see any advantage of using binarySearch comparing with sequential comparison for LinkedList. Of course unless we restrict List to be ArrayList we can do binarySearch with more confidence.

Is my understanding correct? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:24:22+00:00Added an answer on May 28, 2026 at 1:24 pm

    There are many ways to implement the List. There’s ArrayList, LinkedList, CopyOnWriteArrayList, etc. in the standard Java libraries, and a host of other implementations beyond those (VLists, circular buffers, skew binomial lists, extendible arrays, 2-3 finger trees, etc.). The idea behind providing binary search is that while not all List implementations support random access, the ones that do would benefit from having a generic implementation of binary search available so that the authors of each data structure don’t have to reimplement it from scratch. For example, if I implement a crazy new list structure that supports random access, if I implement the List interface I can automatically get a binary search available from the Collections class.

    Interestingly, the binarySearch method is written such that it looks at the type of the List and sees whether it implements the RandomAccess interface before it actually does the binary search. If the list doesn’t implement RandomAccess, the instead of using a standard binary search, the method uses a modified binary search with iterators that is guaranteed to make at most O(n) iterations and O(log n) comparisons. The idea is to keep track of where the last probe landed, then to walk forward or backward the appropriate number of steps to find the next probe location, etc. The total work done is then at most n/2 + n/4 + n/8 + n/16 + … = 2n, so in the worst-case it’s only twice as bad as a worst-case linear search.

    In short, providing a generic implementation of binarySearch doesn’t always make it possible to quickly search a List for something, but for the structures that do support fast access it can make a huge difference and save a lot of implementer time. Additionally, having the graceful degradation to the modified binary search that runs in O(n) time means that the implementation isn’t ever going to be too much worse than a standard linear scan.

    This reasoning is similar to the reasoning behind the design of the C++ algorithms, which operate on generic ranges of values. The efficiency of these algorithms might be much worse than a specialized version of the algorithm on a per-data-structure basis, but having the general version available means that any new containers that support iterators can automatically have a lot of functionality available beyond just what’s specified in the interface.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was thinking about the performance of calling List<T>.Indexof(item) . I am not sure
Not sure what's going on here. I have a DateTime object, and when I
I have three classes that need to be related, and I'm not sure how
I'm new to Django (and databases in general), and I'm not sure how to
I am new to Symfony and am not sure how to best structure my
I think list comprehensions may give me this, but I'm not sure: any elegant
I have a general question about data templates in WPF. Let's say I have
I've never asked anything this general before, and I'm not sure if it's going
Not sure if this is possible or if I'm expressing correctly what I'm looking
Not sure how to ask a followup on SO, but this is in reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.