[GO] Python 2-minute search and its derivation

Introduction

I recently wrote an article about recursive functions. I didn't intend to use it, but it was for studying, but I needed an efficient search. It was saved because I should have just improved the published 2-minute search. Since it's a big deal, I'll publish that the improved function may be useful. (Although I'm actually using Javascript instead of Python, and it has a different implementation)

Find n

python


def binarySearchL(lt, n):
  l, r = 0, len(lt) - 1
  while l <= r:
    middleIndex = int(l + (r - l) / 2)
    middleValue = lt[middleIndex]
    if middleValue > n:
      l, r = l, middleIndex - 1
    elif middleValue < n:
      l, r = middleIndex + 1, r
    else:
      return middleIndex
  return -1

Find the smallest one greater than n

python


def binarySearchGt(lt, n):
  l, r = 0, len(lt) - 1
  while l <= r:
    middleIndex = math.floor(l + (r - l) / 2)  #Truncate
    middleValue = lt[middleIndex]
    if middleValue > n and r - l > 1:
      l, r = l, middleIndex
    elif middleValue <= n:
      l, r = middleIndex + 1, r
    else:
      return middleIndex
  return -1

Find the largest one less than n

python


def binarySearchLt(lt, n):
  l, r = 0, len(lt) - 1
  while l <= r:
    middleIndex = math.ceil(l + (r - l) / 2)  #Round up
    middleValue = lt[middleIndex]
    if middleValue >= n:
      l, r = l, middleIndex - 1
    elif middleValue < n and r - l > 1:
      l, r = middleIndex, r
    else:
      return middleIndex
  return -1

Recommended Posts

Python 2-minute search and its derivation
[Python] Depth-first search and breadth-first search
Search and play YouTube videos in Python
Automatically search and download YouTube videos with Python
Causal reasoning and causal search with Python (for beginners)
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Sequential search with Python
Python Exercise 1-Breadth-first search
Batch design and python
Python iterators and generators
[Python] Search (itertools) ABC167C
Binary search in Python
Python packages and modules
Vue-Cli and Python integration
Binary search (python2.7) memo
[Python] Binary search ABC155D
python input and output
Python and Ruby split
Crawling with Python and Twitter API 1-Simple search function
It's not easy to write Python, it's easy to write numpy and scipy
Find the Hermitian matrix and its eigenvalues in Python
python bit full search
Linear search in Python
Binary search with python
Search Twitter using Python
Recursively search for files and directories in Python and output
Python3, venv and Ansible
Binary search in Python (binary search)
Python asyncio and ContextVar
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) in python
Application to display and search local memos (diary) in Python
Programming with Python and Tkinter
[Python] BFS (breadth-first search) ABC168D
Python: Class and instance variables
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Understand Python packages and modules
# 2 [python3] Separation and comment out
Search for strings in Python
Python shallow copy and deep copy
Python and ruby slice memo
Python installation and basic grammar
Breadth-first search / bidirectional search (Python version)
I compared Java and Python!
Asymptotic theory and its simulation (2)
Homebrew Python --Youtube Search Program
[Python] DFS (Depth-first Search) ATC001A
Binary search in Python / C ++
Algorithm in Python (binary search)
About Python, len () and randint ()
About Python datetime and timezone
Full bit search with Python