-
Notifications
You must be signed in to change notification settings - Fork 76
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
1 parent
2260f05
commit 97111a4
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
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,15 @@ | ||
int indexSearch(int *array, int left, int right) { | ||
if (left > right) { | ||
// value not found | ||
return -1; | ||
} | ||
|
||
int mid = right - (right - left) / 2; | ||
if (array[mid] == mid) { | ||
return mid; | ||
} else if (array[mid] < mid) { | ||
return indexSearch(array, mid + 1, right); | ||
} else { | ||
return indexSearch(array, left, mid - 1); | ||
} | ||
} |
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,16 @@ | ||
bool isElementInMatrix(int **matrix, int M, int N, int target) { | ||
int row = 0; | ||
int column = N - 1; | ||
|
||
while (row < M && column >= 0) { | ||
if (matrix[row][column] == target) { | ||
return true; | ||
} else if (matrix[row][column] < target) { | ||
row++; | ||
} else { | ||
column--; | ||
} | ||
} | ||
return false; | ||
} | ||
|
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,35 @@ | ||
void searchRangeHelper(int array[], int left, int right, int target, int &begin, int &end) { | ||
if (left > right) { | ||
return; | ||
} | ||
|
||
int mid = right - (right - left) / 2; | ||
if (array[mid] == target) { | ||
if (mid < begin || begin == -1) { | ||
begin = mid; | ||
} | ||
if (mid > end) { | ||
end = mid; | ||
} | ||
searchRangeHelper(array, left, mid - 1, target, begin, end); | ||
searchRangeHelper(array, mid + 1, right, target, begin, end); | ||
} | ||
else if (array[mid] < target) { | ||
searchRangeHelper(array, mid + 1, right, target, begin, end); | ||
} | ||
else { | ||
searchRangeHelper(array, left, mid - 1, target, begin, end); | ||
} | ||
} | ||
|
||
vector<int> searchRange(int A[], int n, int target) { | ||
int begin = -1, end = -1; | ||
|
||
searchRangeHelper(A, 0, n - 1, target, begin, end); | ||
|
||
vector<int> ans; | ||
ans.push_back(begin); | ||
ans.push_back(end); | ||
return ans; | ||
} | ||
|
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,26 @@ | ||
int rotated_binary_search(int A[], int N, int key) { | ||
int L = 0; | ||
int R = N - 1; | ||
|
||
while (L <= R) { | ||
// Avoid overflow, same as M=(L+R)/2 | ||
int M = L + ((R - L) >> 1); | ||
if (A[M] == key) return M; | ||
|
||
// the bottom half is sorted | ||
if (A[L] <= A[M]) { | ||
if (A[L] <= key && key < A[M]) | ||
R = M - 1; | ||
else | ||
L = M + 1; | ||
} | ||
// the upper half is sorted | ||
else { | ||
if (A[M] < key && key <= A[R]) | ||
L = M + 1; | ||
else | ||
R = M - 1; | ||
} | ||
} | ||
return -1; | ||
} |