forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
324 additions
and
109 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# find the first node of a cycle in the linked list. | ||
|
||
# 1 -> 2 -> 3 -> 4 -> 5 -> 1 => 1 | ||
# A -> B -> C -> D -> E -> C => C | ||
|
||
def firstCyclicNode(): | ||
pass | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class Node(): | ||
def __init__(self, val = None): | ||
self.val = val | ||
self.next = None | ||
|
||
def printKthToLast(head): | ||
""" | ||
Time Complexity: O() | ||
Space Complexity: O() | ||
""" | ||
pass | ||
|
||
def printLinkedList(head): | ||
string = "" | ||
while head.next: | ||
string += head.val + " -> " | ||
head = head.next | ||
string += head.val | ||
print(string) | ||
|
||
# A A B C D C F G | ||
|
||
a1 = Node("A") | ||
a2 = Node("A") | ||
b = Node("B") | ||
c1 = Node("C") | ||
d = Node("D") | ||
c2 = Node("C") | ||
f = Node("F") | ||
g = Node("G") | ||
|
||
a1.next = a2 | ||
a2.next = b | ||
b.next = c1 | ||
c1.next = d | ||
d.next = c2 | ||
c2.next = f | ||
f.next = g | ||
|
||
# removeDups(a1) | ||
# printLinkedList(a1) | ||
# removeDupsWithoutSet(a1) | ||
# printLinkedList(a1) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
class Node(): | ||
def __init__(self, val = None): | ||
self.val = val | ||
self.next = None | ||
|
||
def removeDups(head): | ||
""" | ||
Time Complexity: O(N) | ||
Space Complexity: O(N) | ||
""" | ||
hashset = set() | ||
prev = Node() | ||
while head: | ||
if head.val in hashset: | ||
prev.next = head.next | ||
else: | ||
hashset.add(head.val) | ||
prev = head | ||
head = head.next | ||
|
||
def removeDupsWithoutSet(head): | ||
""" | ||
Time Complexity: O(N^2) | ||
Space Complexity: O(1) | ||
""" | ||
current = head | ||
while current: | ||
runner = current | ||
while runner.next: | ||
if runner.next.val == current.val: | ||
runner.next = runner.next.next | ||
else: | ||
runner = runner.next | ||
current = current.next | ||
|
||
def printLinkedList(head): | ||
string = "" | ||
while head.next: | ||
string += head.val + " -> " | ||
head = head.next | ||
string += head.val | ||
print(string) | ||
|
||
# A A B C D C F G | ||
|
||
a1 = Node("A") | ||
a2 = Node("A") | ||
b = Node("B") | ||
c1 = Node("C") | ||
d = Node("D") | ||
c2 = Node("C") | ||
f = Node("F") | ||
g = Node("G") | ||
|
||
a1.next = a2 | ||
a2.next = b | ||
b.next = c1 | ||
c1.next = d | ||
d.next = c2 | ||
c2.next = f | ||
f.next = g | ||
|
||
removeDups(a1) | ||
printLinkedList(a1) | ||
removeDupsWithoutSet(a1) | ||
printLinkedList(a1) | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def insertion_sort(arr): | ||
""" Insertion Sort | ||
Complexity: O(n^2) | ||
""" | ||
for i in xrange(len(arr)): | ||
cursor = arr[i] | ||
pos = i | ||
while pos > 0 and arr[pos-1] > cursor: | ||
# Swap the number down the list | ||
arr[pos] = arr[pos-1] | ||
pos = pos-1 | ||
# Break and do the final swap | ||
arr[pos] = cursor | ||
return arr |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
|
||
def merge_sort(arr): | ||
""" Merge Sort | ||
Complexity: O(n log(n)) | ||
""" | ||
size = len(arr) | ||
half = size/2 | ||
# Our recursive base case | ||
if size <= 1: | ||
return arr | ||
# Perform merge_sort recursively on both halves | ||
left, right = merge_sort(arr[half:]), merge_sort(arr[:half]) | ||
|
||
# Merge each side together | ||
return merge(left, right) | ||
|
||
def merge(left, right): | ||
""" Merge helper | ||
Complexity: O(n) | ||
""" | ||
arr = [] | ||
left_cursor, right_cursor = 0,0 | ||
while left_cursor < len(left) and right_cursor < len(right): | ||
# Sort each one and place into the result | ||
if left[left_cursor] <= right[right_cursor]: | ||
arr.append(left[left_cursor]) | ||
left_cursor+=1 | ||
else: | ||
arr.append(right[right_cursor]) | ||
right_cursor+=1 | ||
# Add the left overs if there's any left to the result | ||
if left: | ||
arr.extend(left[left_cursor:]) | ||
if right: | ||
arr.extend(right[right_cursor:]) | ||
return arr | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
array = [1,5, 7,4,3,2,1,9,0,10,43,64] | ||
|
||
|
||
def quick_sort(arr, first, last): | ||
""" Quicksort | ||
Complexity: O(n log(n)) | ||
""" | ||
if first < last: | ||
pos = partition(arr, first, last) | ||
# Start our two recursive calls | ||
quick_sort(arr, first, pos-1) | ||
quick_sort(arr, pos+1, last) | ||
|
||
def partition(arr, first, last): | ||
pivot = first | ||
for pos in xrange(first, last): | ||
if arr[pos] < arr[last]: | ||
arr[pos], arr[pivot] = arr[pivot], arr[pos] | ||
pivot += 1 | ||
arr[pivot], arr[last] = arr[last], arr[pivot] | ||
return pivot | ||
|
||
print(array) | ||
print(quick_sort(array, 0, len(array)-1)) | ||
print(array) |
Oops, something went wrong.