guides
Sorting and Searching
When Pyvorin helps with custom sorting, binary search, and lookup tables.
Published May 30, 2026
QuickSort
def quicksort(arr: list[int]) -> list[int]:
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
Binary Search
def binary_search(arr: list[int], target: int) -> int:
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
When to Use Built-ins
sorted() and list.sort() are C-implemented and already fast. Use Pyvorin for custom comparison logic or multi-key sorts.