Last active
January 6, 2019 10:53
-
-
Save lironsade/4e20686c2950826324f70b26b576e4fe to your computer and use it in GitHub Desktop.
Important algorithms for interviews
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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