Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add @imkaka as a contributor * day4 * Month End
- Loading branch information
1 parent
016bb3a
commit e87a474
Showing
9 changed files
with
396 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
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,55 @@ | ||
/* | ||
* @author : imkaka | ||
* @date : 31/1/2019 | ||
*/ | ||
|
||
#include<iostream> | ||
#include<cstdio> | ||
|
||
using namespace std; | ||
|
||
int binary_search_itr(int arr[], int size, int val){ | ||
int l = 0, r = size-1; | ||
int mid = (l + r) / 2; | ||
|
||
while(arr[mid] != val && l <= r){ | ||
if(val < arr[mid]){ | ||
r = mid - 1; | ||
} | ||
else{ | ||
l = mid +1; | ||
} | ||
|
||
mid = (l + r) / 2; | ||
} | ||
|
||
if(arr[mid] == val) | ||
return mid; | ||
|
||
return -1; | ||
} | ||
|
||
int binary_search_rec(int arr[], int left, int right, int val){ | ||
if(right >= left){ | ||
|
||
int mid = left + (right - left) / 2; | ||
|
||
if(arr[mid] == val) return mid; | ||
|
||
if(arr[mid] > val) | ||
return binary_search_rec(arr, left, mid-1, val); | ||
return binary_search_rec(arr, mid+1, right, val); | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int main(){ | ||
|
||
int arr[] = {1, 2, 3, 6, 9, 15, 16, 14}; | ||
int val = 9; | ||
int size = sizeof(arr)/sizeof(arr[0]); | ||
cout << "Iterative: " << binary_search_itr(arr, size, val) << endl; | ||
cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl; | ||
return 0; | ||
} |
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
Binary file not shown.
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,78 @@ | ||
/* | ||
* @author : imkaka | ||
* @date : 1/2/2019 | ||
*/ | ||
|
||
#include<iostream> | ||
#include<string> | ||
|
||
using namespace std; | ||
|
||
void computeLPS(string, int, int []); | ||
|
||
// KMP Algorithm | ||
void KMPsearch(string text, string pat){ | ||
// Length | ||
int N = text.size(); | ||
int M = pat.size(); | ||
|
||
// Define LPS (Longest Proper Prefix) | ||
int lps[M]; | ||
|
||
//Preprocess | ||
computeLPS(pat, M, lps); | ||
|
||
int i = 0, j = 0; | ||
while(i < N){ | ||
//While Match | ||
if(pat[j] == text[i]){ | ||
i++; | ||
j++; | ||
} | ||
|
||
if(j == M){ | ||
cout << "Pattern Found At " << (i-j) << endl; | ||
j = lps[j-1]; | ||
} | ||
|
||
else if(i < N && pat[j] != text[i]){ | ||
if(lps[j] != 0) | ||
j = lps[j-1]; | ||
else | ||
i++; | ||
} | ||
} | ||
} | ||
|
||
void computeLPS(string pat, int M, int lps[]){ | ||
int len = 0; //Track len of longest common prefix which is suffix also. | ||
|
||
lps[0] = 0; | ||
int i = 1; | ||
|
||
while(i < M){ | ||
if(pat[i] == pat[len]){ | ||
len++; | ||
lps[i] = len; | ||
i++; | ||
} | ||
else{ | ||
if(len != 0){ | ||
len = lps[len-1]; //Don't increment i | ||
} | ||
else{ | ||
lps[i] = 0; | ||
i++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
|
||
string txt = "ABABDABACDABABCABAB"; | ||
string pat = "ABABCABAB"; | ||
|
||
KMPsearch(txt, pat); | ||
return 0; | ||
} |
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
Binary file not shown.
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,53 @@ | ||
/* | ||
* @author : imkaka | ||
* @date : 1/2/2019 | ||
* | ||
*/ | ||
|
||
#include<iostream> | ||
#include<vector> | ||
|
||
using namespace std; | ||
|
||
void bubblesort(vector<int>& arr){ | ||
int N = arr.size(); | ||
|
||
bool swapped; | ||
for(int i = 0; i < N-1; ++i){ | ||
swapped = false; // Optimizing part | ||
for(int j = 0; j < N-i-1; ++j) // (j, j+1) Adjacent Swap. | ||
|
||
if(arr[j] > arr[j+1]){ // Swap Part | ||
int temp = arr[j]; | ||
arr[j] = arr[j+1]; | ||
arr[j+1] = temp; | ||
swapped = true; | ||
} | ||
if(!swapped) | ||
break; | ||
} | ||
} | ||
|
||
void printArray(const vector<int> &arr){ | ||
int N = arr.size(); | ||
|
||
for(const int x : arr){ | ||
cout << x << " "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
int main(){ | ||
|
||
vector<int> arr = {20, 10, 0, 36, 100, 12, -20, 30, 50, -100}; | ||
|
||
cout << "Unsorted:" << endl; | ||
printArray(arr); | ||
|
||
bubblesort(arr); | ||
|
||
cout << "Sorted: " << endl; | ||
printArray(arr); | ||
return 0; | ||
} | ||
|
Oops, something went wrong.