Skip to content

Commit fab5ff7

Browse files
Karthikn2099MadhavBahl
authored andcommitted
C++ code for Linear and Binary Search been added (#218)
* factorial in c++ * fibonacci recursion * c++ code for day14 * C++ solution for day15 added * day1 FizzBuzz Code in C++ * C++ code for day2 * day 28 c++ solution * binary search algorithm for sorted array
1 parent 86da255 commit fab5ff7

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

day28/C++/Linear_Search.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author: Karthick < karthikn2099@gmail.com >
3+
* @github: https://github.com/karthikn2098
4+
* @date: 31/01/2018
5+
*/
6+
7+
#include <iostream>
8+
using namespace std;
9+
10+
/** @desc: compares the key with each array elements.
11+
* @param: array , key (to be found), size of the array.
12+
* @return: index of key if present , -1 otherwise.
13+
* @TimeComplexity: O(n) < n = size of the array >
14+
*/
15+
int linear_search(int* arr , int key, int N) {
16+
17+
for( int i = 0 ; i < N ; i++ ) {
18+
if(arr[i] == key) {
19+
return i;
20+
}
21+
}
22+
23+
return -1;
24+
}
25+
26+
int main(void) {
27+
28+
int arr[] = { 12, 15, 1, 16, 20, 100, 89, 97, 205, 345, 467 };
29+
30+
int sizeOfArray = sizeof(arr) / sizeof(arr[0]);
31+
32+
cout << "\nLinear_search(arr , 20) = " << linear_search(arr, 20, sizeOfArray ) << endl;
33+
34+
cout << "\nLinear_search(arr , 1000) = " << linear_search(arr, 1000, sizeOfArray ) << endl;
35+
36+
return 0;
37+
}
38+
39+
/*
40+
---------------
41+
Sample Output
42+
---------------
43+
44+
Linear_search(arr , 20) = 4
45+
46+
Linear_search(arr , 1000) = -1
47+
48+
*/

day28/C++/day28_solved.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Author: Shubhendra Singh
3+
Github: shubhendra-20
4+
*/
5+
6+
#include <iostream>
7+
using namespace std;
8+
9+
int main() {
10+
int i,j,n,x,flag;
11+
12+
cin>>n;
13+
int a[n];
14+
for(i=0;i<n;i++)
15+
{
16+
cin>>a[i];
17+
}
18+
cin>>x;
19+
flag=0;
20+
for(i=0;i<n;i++)
21+
{
22+
if(a[i]==x)
23+
{
24+
flag=1;
25+
cout<<(i+1)<<endl;
26+
break;
27+
}
28+
}
29+
if(flag==0)
30+
{
31+
cout<<"undefined";
32+
}
33+
return 0;
34+
}

day29/C++/Binary_Search.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @author: Karthick < karthikn2099@gmail.com >
3+
* @github: https://github.com/karthikn2098
4+
* @date: 31/01/2018
5+
*/
6+
7+
#include <iostream>
8+
using namespace std;
9+
10+
/** @desc: splits the array into two sections at each step, then checks the element in the desired one.
11+
* @param: array , key (to be found), size of the array.
12+
* @return: index of key if present , -1 otherwise.
13+
* @TimeComplexity: O( logn ) < n = size of the array >
14+
*/
15+
int Binary_search(int arr[] , int key, int N) {
16+
17+
int low = 0 , high = N-1 , mid;
18+
19+
while(low <= high) {
20+
21+
mid = low + (high - low) / 2; //like (low + high)/2 but efficient.
22+
23+
if(key == arr[mid]) {
24+
return mid;
25+
}
26+
else if( key > arr[mid] ) {
27+
low = mid + 1;
28+
}
29+
else {
30+
high = mid - 1;
31+
}
32+
}
33+
34+
return -1;
35+
}
36+
37+
int main(void) {
38+
39+
int arr[] = { 1, 3, 5, 7, 8, 9, 100, 234, 788, 899 };
40+
41+
int sizeOfArray = sizeof(arr) / sizeof(arr[0]);
42+
43+
cout << "\nBinary_search(arr , 100) = " << Binary_search(arr, 100, sizeOfArray ) << endl;
44+
45+
cout << "\nBinary_search(arr , 1000) = " << Binary_search(arr, 1000, sizeOfArray ) << endl;
46+
47+
return 0;
48+
}
49+
50+
/*
51+
---------------
52+
Sample Output
53+
---------------
54+
55+
Binary_search(arr , 100) = 6
56+
57+
Binary_search(arr , 1000) = -1
58+
59+
*/

0 commit comments

Comments
 (0)