From 4968dd62a794717fd3e45fd6754da358b0746f21 Mon Sep 17 00:00:00 2001 From: Divyanshmandhan-1 <70142854+Divyanshmandhan-1@users.noreply.github.com> Date: Sun, 17 Jan 2021 17:32:30 +0530 Subject: [PATCH 1/4] Create longest_sum_contigous_array.py --- .../arrays/longest_sum_contigous_array.py | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 algorithms/arrays/longest_sum_contigous_array.py diff --git a/algorithms/arrays/longest_sum_contigous_array.py b/algorithms/arrays/longest_sum_contigous_array.py new file mode 100644 index 000000000..40cd5a448 --- /dev/null +++ b/algorithms/arrays/longest_sum_contigous_array.py @@ -0,0 +1,64 @@ +""" +Find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. + +The idea of algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). +And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). +Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far + +Example) +Let input array = [-2,-3,4,-1,-2,1,5,3] + +max_so_far = max_ending_here = 0 + +for i=0, a[0] = -2 +max_ending_here = max_ending_here + (-2) +Set max_ending_here = 0 because max_ending_here < 0 + +for i=1, a[1] = -3 +max_ending_here = max_ending_here + (-3) +Set max_ending_here = 0 because max_ending_here < 0 + +for i=2, a[2] = 4 +max_ending_here = max_ending_here + (4) +max_ending_here = 4 +max_so_far is updated to 4 because max_ending_here greater +than max_so_far which was 0 till now + +for i=3, a[3] = -1 +max_ending_here = max_ending_here + (-1) +max_ending_here = 3 + +for i=4, a[4] = -2 +max_ending_here = max_ending_here + (-2) +max_ending_here = 1 + +for i=5, a[5] = 1 +max_ending_here = max_ending_here + (1) +max_ending_here = 2 + +for i=6, a[6] = 5 +max_ending_here = max_ending_here + (5) +max_ending_here = 7 +max_so_far is updated to 7 because max_ending_here is +greater than max_so_far + +for i=7, a[7] = -3 +max_ending_here = max_ending_here + (-3) +max_ending_here = 4 + +4+(-1)+(-2)+1+5 = 7 +hence,maximum contiguous array sum is 7 +""" +def max_subarray_sum(a,size): + + max_so_far = 0 + max_ending_here = 0 + + for i in range(0, size): + max_ending_here = max_ending_here + a[i] + if max_ending_here < 0: + max_ending_here = 0 + elif (max_so_far < max_ending_here): + max_so_far = max_ending_here + + return max_so_far From 77cfed5c2a67cb81d3f3cf3650f190231d0b932e Mon Sep 17 00:00:00 2001 From: Divyanshmandhan-1 <70142854+Divyanshmandhan-1@users.noreply.github.com> Date: Mon, 18 Jan 2021 10:50:44 +0530 Subject: [PATCH 2/4] add ternary search --- algorithms/search/ternary_search.py | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 algorithms/search/ternary_search.py diff --git a/algorithms/search/ternary_search.py b/algorithms/search/ternary_search.py new file mode 100644 index 000000000..b7d36fb21 --- /dev/null +++ b/algorithms/search/ternary_search.py @@ -0,0 +1,37 @@ +""" +Ternary search is a divide and conquer algorithm that can be used to find an element in an array. +It is similar to binary search where we divide the array into two parts but in this algorithm, +we divide the given array into three parts and determine which has the key (searched element). +We can divide the array into three parts by taking mid1 and mid2. +Initially, l and r will be equal to 0 and n-1 respectively, where n is the length of the array. +mid1 = l + (r-l)/3 +mid2 = r – (r-l)/3 + +Note: Array needs to be sorted to perform ternary search on it. +T(N) = O(log3(N)) +log3 = log base 3 +""" +def ternary_search(l, r, key, arr): + while r >= l: + + mid1 = l + (r-l) // 3 + mid2 = r - (r-l) // 3 + + if key == arr[mid1]: + return mid1 + if key == mid2: + return mid2 + + if key < arr[mid1]: + # key lies between l and mid1 + r = mid1 - 1 + elif key > arr[mid2]: + # key lies between mid2 and r + l = mid2 + 1 + else: + # key lies between mid1 and mid2 + l = mid1 + 1 + r = mid2 - 1 + + # key not found + return -1 From 7a1e7250d248cb098db6b06e28df50564006f806 Mon Sep 17 00:00:00 2001 From: Divyanshmandhan-1 <70142854+Divyanshmandhan-1@users.noreply.github.com> Date: Mon, 18 Jan 2021 10:59:07 +0530 Subject: [PATCH 3/4] Delete longest_sum_contigous_array.py --- .../arrays/longest_sum_contigous_array.py | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 algorithms/arrays/longest_sum_contigous_array.py diff --git a/algorithms/arrays/longest_sum_contigous_array.py b/algorithms/arrays/longest_sum_contigous_array.py deleted file mode 100644 index 40cd5a448..000000000 --- a/algorithms/arrays/longest_sum_contigous_array.py +++ /dev/null @@ -1,64 +0,0 @@ -""" -Find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. - -The idea of algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). -And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). -Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far - -Example) -Let input array = [-2,-3,4,-1,-2,1,5,3] - -max_so_far = max_ending_here = 0 - -for i=0, a[0] = -2 -max_ending_here = max_ending_here + (-2) -Set max_ending_here = 0 because max_ending_here < 0 - -for i=1, a[1] = -3 -max_ending_here = max_ending_here + (-3) -Set max_ending_here = 0 because max_ending_here < 0 - -for i=2, a[2] = 4 -max_ending_here = max_ending_here + (4) -max_ending_here = 4 -max_so_far is updated to 4 because max_ending_here greater -than max_so_far which was 0 till now - -for i=3, a[3] = -1 -max_ending_here = max_ending_here + (-1) -max_ending_here = 3 - -for i=4, a[4] = -2 -max_ending_here = max_ending_here + (-2) -max_ending_here = 1 - -for i=5, a[5] = 1 -max_ending_here = max_ending_here + (1) -max_ending_here = 2 - -for i=6, a[6] = 5 -max_ending_here = max_ending_here + (5) -max_ending_here = 7 -max_so_far is updated to 7 because max_ending_here is -greater than max_so_far - -for i=7, a[7] = -3 -max_ending_here = max_ending_here + (-3) -max_ending_here = 4 - -4+(-1)+(-2)+1+5 = 7 -hence,maximum contiguous array sum is 7 -""" -def max_subarray_sum(a,size): - - max_so_far = 0 - max_ending_here = 0 - - for i in range(0, size): - max_ending_here = max_ending_here + a[i] - if max_ending_here < 0: - max_ending_here = 0 - elif (max_so_far < max_ending_here): - max_so_far = max_ending_here - - return max_so_far From ef63e5329a02022d1f2a0dea27af9b367f6e2cf8 Mon Sep 17 00:00:00 2001 From: Divyanshmandhan-1 <70142854+Divyanshmandhan-1@users.noreply.github.com> Date: Thu, 28 Jan 2021 12:50:58 +0530 Subject: [PATCH 4/4] added test cases for ternary search --- algorithms/search/__init__.py | 1 + tests/test_search.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/algorithms/search/__init__.py b/algorithms/search/__init__.py index 2586605e1..2226e4b5d 100644 --- a/algorithms/search/__init__.py +++ b/algorithms/search/__init__.py @@ -1,4 +1,5 @@ from .binary_search import * +from .ternary_search import * from .first_occurrence import * from .last_occurrence import * from .linear_search import * diff --git a/tests/test_search.py b/tests/test_search.py index 3f6c23f3e..d1ea8225d 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1,5 +1,6 @@ from algorithms.search import ( binary_search, binary_search_recur, + ternary_search, first_occurrence, last_occurrence, linear_search, @@ -41,6 +42,15 @@ def test_binary_search(self): self.assertEqual(11, binary_search_recur(array, 0, 11, 6)) self.assertEqual(-1, binary_search_recur(array, 0, 11, 7)) self.assertEqual(-1, binary_search_recur(array, 0, 11, -1)) + + def test_ternary_search(self): + array = [1, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6] + self.assertEqual(10, ternary_search(0, 11, 5, array)) + self.assertEqual(3, ternary_search(0, 10, 3, array)) + self.assertEqual(-1, ternary_search(0, 10, 5, array)) + self.assertEqual(-1, ternary_search(0, 11, 7, array)) + self.assertEqual(-1, ternary_search(0, 11, -1, array)) + def test_last_occurrence(self): array = [1, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 6, 6]