Browse Source

Add binary search, unify naming and folder structure

Snow 8 years ago
parent
commit
289e8bd952

arrays-and-strings/matrix-zeros.py → arrays-and-strings/matrix_zeros.py


arrays-and-strings/min-window.py → arrays-and-strings/min_window.py


+ 2 - 2
arrays-and-strings/remove-duplicates.py

@@ -16,8 +16,8 @@ def test_remove_duplicates():
 pytest.main()
 
 # use two indicies to track non-duplicates in place.
-# time complexity: O(n^2)
-# space complexity: O(n)
+# time complexity: O(n)
+# space complexity: O(1)
 def remove_duplicates(input):
     len_input = len(input)
     if len_input <= 1:

arrays-and-strings/string-replacement.py → arrays-and-strings/string_replacement.py


+ 23 - 0
search-and-sort/binary_search.py

@@ -0,0 +1,23 @@
+# Time complexity: O(logn)
+# Space complexity: O(1)
+
+import pytest
+
+def test_binary_search():
+    case1 = [0,1,2,3]
+    assert binary_search(case1,1) == 1
+
+def binary_search(l, target):
+    left = 0
+    right = len(l)-1
+
+    while right > left:
+        m = (right - left )//2
+        print(m)
+        if l[m] == target:
+            return m
+        elif target < l[m]:
+            right = m - 1
+        else:
+            left = m + 1
+    return 0

arrays-and-strings/merge-sort.py → search-and-sort/merge_sort.py


arrays-and-strings/quick-sort.py → search-and-sort/quick_sort.py