Linear Search
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
Core logic snippets only — read, copy, learn. (Two slides per row on desktop.)
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
while left <= right:
mid1 = left + (right - left) // 3
mid2 = right - (right - left) // 3
if arr[mid1] == target:
return mid1
elif arr[mid2] == target:
return mid2
elif target < arr[mid1]:
right = mid1 - 1
elif target > arr[mid2]:
left = mid2 + 1
else:
left = mid1 + 1
right = mid2 - 1
step = int(sqrt(n))
prev = 0
while prev < n and arr[min(step, n)-1] < target:
prev = step
step += int(sqrt(n))
if prev >= n:
break
# then linear search in block
while low <= high and target >= arr[low] and target <= arr[high]:
pos = low + int(((target-arr[low])*(high-low)) / (arr[high]-arr[low]))
if arr[pos] == target:
return pos
elif arr[pos] < target:
low = pos + 1
else:
high = pos - 1
if arr[0] == target:
return 0
index = 1
while index < n and arr[index] <= target:
index *= 2
# then binary search in range [index/2, min(index,n-1)]
for i in range(n-1):
for j in range(n-1-i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]; j -= 1
arr[j+1] = key
# after splitting L and R
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]: arr[k] = L[i]; i += 1
else: arr[k] = R[j]; j += 1
k += 1
arr[k:] = L[i:] + R[j:]
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1; arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
largest = root
left = 2*root + 1
right = 2*root + 2
if left < n and arr[left] > arr[largest]: largest = left
if right < n and arr[right] > arr[largest]: largest = right
if largest != root:
arr[root], arr[largest] = arr[largest], arr[root]
heapify(n, largest)
count = [0] * (max_val + 1) for x in arr: count[x] += 1 for i in range(1, len(count)): count[i] += count[i-1] # build output by placing items using counts (stable)
exp = 1
while max_val//exp > 0:
# stable counting sort by digit (arr[i]//exp % 10)
exp *= 10
gap = n//2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j-gap] > temp:
arr[j] = arr[j-gap]
j -= gap
arr[j] = temp
gap //= 2
def fib(n):
if n < 2: return n
return fib(n-1) + fib(n-2)
def fact(n):
if n <= 1: return 1
return n * fact(n-1)
def inorder(node):
if node:
inorder(node.left)
print(node.val)
inorder(node.right)
from collections import deque
q = deque([start]); visited.add(start)
while q:
node = q.popleft()
for nbr in graph[node]:
if nbr not in visited:
visited.add(nbr); q.append(nbr)
def dfs(u):
visited.add(u)
for v in graph[u]:
if v not in visited: dfs(v)
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statementldmbkdsbkksb
dzlbk zdk
,blxmbk xzkbb
dzmbk
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement
if condition:
statement
else:
other_statement