Skip to content

Instantly share code, notes, and snippets.

@lironsade
Last active January 6, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lironsade/4e20686c2950826324f70b26b576e4fe to your computer and use it in GitHub Desktop.
Save lironsade/4e20686c2950826324f70b26b576e4fe to your computer and use it in GitHub Desktop.
Important algorithms for interviews
def mergeSort(A):
mid = len(A) // 2
if len(A) > 1:
left = mergeSort(A[:mid])
right = mergeSort(A[mid:])
# merge
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
A[k] = left[i]
i += 1
else:
A[k] = right[j]
j += 1
k += 1
while i < len(left):
A[k] = left[i]
i += 1
k += 1
while j < len(right):
A[k] = right[j]
j += 1
k += 1
return A
def partition(A, first, last):
pivot = A[last]
lefti = first + 1
righti = last
done = False
while not done:
while lefti <= righti and A[lefti] <= pivot:
lefti += 1
while A[righti] >= pivot and righti >= lefti:
righti -= 1
if righti < lefti:
done =True
else:
temp = A[lefti]
A[lefti] = A[righti]
A[righti] = temp
temp = A[lefti]
A[lefti] = A[righti]
A[righti] = temp
return righti
def quickSortHelper(A, left, right):
if left < right:
ind = partition(A, left, right)
quickSortHelper(A, left, ind - 1)
quickSortHelper(A, i + 1, right)
def quickSort(A):
return quickSortHelper(A, 0, len(A) - 1)
def BFS(G):
pass
def DFS(G):
pass
def binarySearch(A, x):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment