Skip to content

Commit e87a474

Browse files
imkakaMadhavBahl
authored andcommitted
Day 29, 30 & 31 (#221)
* Add @imkaka as a contributor * day4 * Month End
1 parent 016bb3a commit e87a474

File tree

9 files changed

+396
-5
lines changed

9 files changed

+396
-5
lines changed

day29/C++/binary_search

12.9 KB
Binary file not shown.

day29/C++/binary_search.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @author : imkaka
3+
* @date : 31/1/2019
4+
*/
5+
6+
#include<iostream>
7+
#include<cstdio>
8+
9+
using namespace std;
10+
11+
int binary_search_itr(int arr[], int size, int val){
12+
int l = 0, r = size-1;
13+
int mid = (l + r) / 2;
14+
15+
while(arr[mid] != val && l <= r){
16+
if(val < arr[mid]){
17+
r = mid - 1;
18+
}
19+
else{
20+
l = mid +1;
21+
}
22+
23+
mid = (l + r) / 2;
24+
}
25+
26+
if(arr[mid] == val)
27+
return mid;
28+
29+
return -1;
30+
}
31+
32+
int binary_search_rec(int arr[], int left, int right, int val){
33+
if(right >= left){
34+
35+
int mid = left + (right - left) / 2;
36+
37+
if(arr[mid] == val) return mid;
38+
39+
if(arr[mid] > val)
40+
return binary_search_rec(arr, left, mid-1, val);
41+
return binary_search_rec(arr, mid+1, right, val);
42+
}
43+
44+
return -1;
45+
}
46+
47+
int main(){
48+
49+
int arr[] = {1, 2, 3, 6, 9, 15, 16, 14};
50+
int val = 9;
51+
int size = sizeof(arr)/sizeof(arr[0]);
52+
cout << "Iterative: " << binary_search_itr(arr, size, val) << endl;
53+
cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl;
54+
return 0;
55+
}

day29/README.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,69 @@ function binary (arr, n) {
4141
return -1;
4242
}
4343

44-
console.log (binary ([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)); // 4
45-
```
44+
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 8));
45+
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 7));
46+
```
47+
48+
## C++ Implementation
49+
50+
### [Solution by @imkaka](./C++/binary_search.cpp)
51+
52+
```cpp
53+
54+
/*
55+
* @author : imkaka
56+
* @date : 31/1/2019
57+
*/
58+
59+
#include<iostream>
60+
#include<cstdio>
61+
62+
using namespace std;
63+
64+
int binary_search_itr(int arr[], int size, int val){
65+
int l = 0, r = size-1;
66+
int mid = (l + r) / 2;
67+
68+
while(arr[mid] != val && l <= r){
69+
if(val < arr[mid]){
70+
r = mid - 1;
71+
}
72+
else{
73+
l = mid +1;
74+
}
75+
76+
mid = (l + r) / 2;
77+
}
78+
79+
if(arr[mid] == val)
80+
return mid;
81+
82+
return -1;
83+
}
84+
85+
int binary_search_rec(int arr[], int left, int right, int val){
86+
if(right >= left){
87+
88+
int mid = left + (right - left) / 2;
89+
90+
if(arr[mid] == val) return mid;
91+
92+
if(arr[mid] > val)
93+
return binary_search_rec(arr, left, mid-1, val);
94+
return binary_search_rec(arr, mid+1, right, val);
95+
}
96+
97+
return -1;
98+
}
99+
100+
int main(){
101+
102+
int arr[] = {1, 2, 3, 6, 9, 15, 16, 14};
103+
int val = 9;
104+
int size = sizeof(arr)/sizeof(arr[0]);
105+
cout << "Iterative: " << binary_search_itr(arr, size, val) << endl;
106+
cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl;
107+
return 0;
108+
}
109+
```

day30/C++/naiveSearch

13.9 KB
Binary file not shown.

day30/C++/naiveSearch.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @author : imkaka
3+
* @date : 1/2/2019
4+
*/
5+
6+
#include<iostream>
7+
#include<string>
8+
9+
using namespace std;
10+
11+
void computeLPS(string, int, int []);
12+
13+
// KMP Algorithm
14+
void KMPsearch(string text, string pat){
15+
// Length
16+
int N = text.size();
17+
int M = pat.size();
18+
19+
// Define LPS (Longest Proper Prefix)
20+
int lps[M];
21+
22+
//Preprocess
23+
computeLPS(pat, M, lps);
24+
25+
int i = 0, j = 0;
26+
while(i < N){
27+
//While Match
28+
if(pat[j] == text[i]){
29+
i++;
30+
j++;
31+
}
32+
33+
if(j == M){
34+
cout << "Pattern Found At " << (i-j) << endl;
35+
j = lps[j-1];
36+
}
37+
38+
else if(i < N && pat[j] != text[i]){
39+
if(lps[j] != 0)
40+
j = lps[j-1];
41+
else
42+
i++;
43+
}
44+
}
45+
}
46+
47+
void computeLPS(string pat, int M, int lps[]){
48+
int len = 0; //Track len of longest common prefix which is suffix also.
49+
50+
lps[0] = 0;
51+
int i = 1;
52+
53+
while(i < M){
54+
if(pat[i] == pat[len]){
55+
len++;
56+
lps[i] = len;
57+
i++;
58+
}
59+
else{
60+
if(len != 0){
61+
len = lps[len-1]; //Don't increment i
62+
}
63+
else{
64+
lps[i] = 0;
65+
i++;
66+
}
67+
}
68+
}
69+
}
70+
71+
int main(){
72+
73+
string txt = "ABABDABACDABABCABAB";
74+
string pat = "ABABCABAB";
75+
76+
KMPsearch(txt, pat);
77+
return 0;
78+
}

day30/README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Given a sentence (string), and a pattern, write a function that returns the inde
1111
**Example**
1212

1313
```
14-
input:
14+
input:
1515
str = "Hello World, Coding is beautiful"
1616
pattern = "World"
1717
@@ -26,4 +26,87 @@ output: 6 (start index of the found pattern)
2626

2727
```js
2828
to be added
29-
```
29+
```
30+
31+
### [C++ Implementation](./C++/naiveSearch.cpp)
32+
33+
```cpp
34+
/*
35+
* @author : imkaka
36+
* @date : 1/2/2019
37+
*/
38+
39+
#include<iostream>
40+
#include<string>
41+
42+
using namespace std;
43+
44+
void computeLPS(string, int, int []);
45+
46+
// KMP Algorithm
47+
void KMPsearch(string text, string pat){
48+
// Length
49+
int N = text.size();
50+
int M = pat.size();
51+
52+
// Define LPS (Longest Proper Prefix)
53+
int lps[M];
54+
55+
//Preprocess
56+
computeLPS(pat, M, lps);
57+
58+
int i = 0, j = 0;
59+
while(i < N){
60+
//While Match
61+
if(pat[j] == text[i]){
62+
i++;
63+
j++;
64+
}
65+
66+
if(j == M){
67+
cout << "Pattern Found At " << (i-j) << endl;
68+
j = lps[j-1];
69+
}
70+
71+
else if(i < N && pat[j] != text[i]){
72+
if(lps[j] != 0)
73+
j = lps[j-1];
74+
else
75+
i++;
76+
}
77+
}
78+
}
79+
80+
void computeLPS(string pat, int M, int lps[]){
81+
int len = 0; //Track len of longest common prefix which is suffix also.
82+
83+
lps[0] = 0;
84+
int i = 1;
85+
86+
while(i < M){
87+
if(pat[i] == pat[len]){
88+
len++;
89+
lps[i] = len;
90+
i++;
91+
}
92+
else{
93+
if(len != 0){
94+
len = lps[len-1]; //Don't increment i
95+
}
96+
else{
97+
lps[i] = 0;
98+
i++;
99+
}
100+
}
101+
}
102+
}
103+
104+
int main(){
105+
106+
string txt = "ABABDABACDABABCABAB";
107+
string pat = "ABABCABAB";
108+
109+
KMPsearch(txt, pat);
110+
return 0;
111+
}
112+
```

day31/C++/bubbleSort

21.5 KB
Binary file not shown.

day31/C++/bubbleSort.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @author : imkaka
3+
* @date : 1/2/2019
4+
*
5+
*/
6+
7+
#include<iostream>
8+
#include<vector>
9+
10+
using namespace std;
11+
12+
void bubblesort(vector<int>& arr){
13+
int N = arr.size();
14+
15+
bool swapped;
16+
for(int i = 0; i < N-1; ++i){
17+
swapped = false; // Optimizing part
18+
for(int j = 0; j < N-i-1; ++j) // (j, j+1) Adjacent Swap.
19+
20+
if(arr[j] > arr[j+1]){ // Swap Part
21+
int temp = arr[j];
22+
arr[j] = arr[j+1];
23+
arr[j+1] = temp;
24+
swapped = true;
25+
}
26+
if(!swapped)
27+
break;
28+
}
29+
}
30+
31+
void printArray(const vector<int> &arr){
32+
int N = arr.size();
33+
34+
for(const int x : arr){
35+
cout << x << " ";
36+
}
37+
cout << endl;
38+
}
39+
40+
int main(){
41+
42+
vector<int> arr = {20, 10, 0, 36, 100, 12, -20, 30, 50, -100};
43+
44+
cout << "Unsorted:" << endl;
45+
printArray(arr);
46+
47+
bubblesort(arr);
48+
49+
cout << "Sorted: " << endl;
50+
printArray(arr);
51+
return 0;
52+
}
53+

0 commit comments

Comments
 (0)