Skip to content

Instantly share code, notes, and snippets.

@HYChou0515
Last active August 22, 2021 02:38
Show Gist options
  • Save HYChou0515/67b523614a22f4ea9491473576a88c87 to your computer and use it in GitHub Desktop.
Save HYChou0515/67b523614a22f4ea9491473576a88c87 to your computer and use it in GitHub Desktop.

Tasks involving search in a sorted list are pretty common. Sometimes using list literal would be very convenient and fast enough for small list, e.g., [v for v >= x for v in a].

However, when the list is too large, this kind of linear search can cost much time than it is actually needed. Using binary search is preffered in this situation.

Python has a built in library bisect for this kind of job. Sometimes it could be quite confusing how to use, though.

Here I tried to summary the basic usage and some useful application of bisect.

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment