Skip to content

Commit

Permalink
C++ code for Linear and Binary Search been added (#218)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Karthikn2099 authored and MadhavBahl committed Feb 7, 2019
1 parent 86da255 commit fab5ff7
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
48 changes: 48 additions & 0 deletions day28/C++/Linear_Search.cpp
@@ -0,0 +1,48 @@
/**
* @author: Karthick < karthikn2099@gmail.com >
* @github: https://github.com/karthikn2098
* @date: 31/01/2018
*/

#include <iostream>
using namespace std;

/** @desc: compares the key with each array elements.
* @param: array , key (to be found), size of the array.
* @return: index of key if present , -1 otherwise.
* @TimeComplexity: O(n) < n = size of the array >
*/
int linear_search(int* arr , int key, int N) {

for( int i = 0 ; i < N ; i++ ) {
if(arr[i] == key) {
return i;
}
}

return -1;
}

int main(void) {

int arr[] = { 12, 15, 1, 16, 20, 100, 89, 97, 205, 345, 467 };

int sizeOfArray = sizeof(arr) / sizeof(arr[0]);

cout << "\nLinear_search(arr , 20) = " << linear_search(arr, 20, sizeOfArray ) << endl;

cout << "\nLinear_search(arr , 1000) = " << linear_search(arr, 1000, sizeOfArray ) << endl;

return 0;
}

/*
---------------
Sample Output
---------------
Linear_search(arr , 20) = 4
Linear_search(arr , 1000) = -1
*/
34 changes: 34 additions & 0 deletions day28/C++/day28_solved.cpp
@@ -0,0 +1,34 @@
/*
Author: Shubhendra Singh
Github: shubhendra-20
*/

#include <iostream>
using namespace std;

int main() {
int i,j,n,x,flag;

cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
cin>>x;
flag=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
cout<<(i+1)<<endl;
break;
}
}
if(flag==0)
{
cout<<"undefined";
}
return 0;
}
59 changes: 59 additions & 0 deletions day29/C++/Binary_Search.cpp
@@ -0,0 +1,59 @@
/**
* @author: Karthick < karthikn2099@gmail.com >
* @github: https://github.com/karthikn2098
* @date: 31/01/2018
*/

#include <iostream>
using namespace std;

/** @desc: splits the array into two sections at each step, then checks the element in the desired one.
* @param: array , key (to be found), size of the array.
* @return: index of key if present , -1 otherwise.
* @TimeComplexity: O( logn ) < n = size of the array >
*/
int Binary_search(int arr[] , int key, int N) {

int low = 0 , high = N-1 , mid;

while(low <= high) {

mid = low + (high - low) / 2; //like (low + high)/2 but efficient.

if(key == arr[mid]) {
return mid;
}
else if( key > arr[mid] ) {
low = mid + 1;
}
else {
high = mid - 1;
}
}

return -1;
}

int main(void) {

int arr[] = { 1, 3, 5, 7, 8, 9, 100, 234, 788, 899 };

int sizeOfArray = sizeof(arr) / sizeof(arr[0]);

cout << "\nBinary_search(arr , 100) = " << Binary_search(arr, 100, sizeOfArray ) << endl;

cout << "\nBinary_search(arr , 1000) = " << Binary_search(arr, 1000, sizeOfArray ) << endl;

return 0;
}

/*
---------------
Sample Output
---------------
Binary_search(arr , 100) = 6
Binary_search(arr , 1000) = -1
*/

0 comments on commit fab5ff7

Please sign in to comment.