From 53cb6af0d2925fb3e4a28f94aa1c9c37bb6dd2d1 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Fri, 28 Jun 2013 17:40:54 +0200 Subject: [PATCH] Starts implementing binary search algorithm --- .gitignore | 1 + 00_binary_search/bin_search.py | 21 +++++++++++++++++++++ 00_binary_search/bin_search_test.py | 20 ++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 .gitignore create mode 100644 00_binary_search/bin_search.py create mode 100644 00_binary_search/bin_search_test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e99e36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/00_binary_search/bin_search.py b/00_binary_search/bin_search.py new file mode 100644 index 0000000..579ae69 --- /dev/null +++ b/00_binary_search/bin_search.py @@ -0,0 +1,21 @@ +""" +Binary search implementation +@jlengrand +2013/07 + +Binary search solves the problem [of searching within a pre-sorted array] by +keeping track of a range within the array in which T [i.e. the sought value] +must be if it is anywhere in the array. Initially, the range is the entire +array. +The range is shrunk by comparing its middle element to T and discarding half the +range. The process continues until T is discovered in the array, or until the +range in which it must lie is known to be empty. In an N-element table, +the search uses roughly log(2) N comparisons. +""" + +def bin_search(arr, t): + """ + Performs binary search on the input array. + Searches for t in arr, using a binary search. + Returns t index in arr if it is found, null otherwise + """ \ No newline at end of file diff --git a/00_binary_search/bin_search_test.py b/00_binary_search/bin_search_test.py new file mode 100644 index 0000000..12a554c --- /dev/null +++ b/00_binary_search/bin_search_test.py @@ -0,0 +1,20 @@ +""" +Unit tests for the binary search algorithm +@jlengrand +2013/07 +""" +from bin_search import bin_search + +import unittest + + +class test_bin_search(unittest.TestCase): + + def setUp(self): + arr1 = [1, 2, 3, 4, 5] + + def test_shuffle(self): + self.assertTrue(1, 1) + +if __name__ == '__main__': + unittest.main()