Added c++ implementation (#85)
* Added c++ implementation * Added Bfs implementation in c++
This commit is contained in:
40
01_introduction_to_algorithms/c++/01_binary_search.cpp
Normal file
40
01_introduction_to_algorithms/c++/01_binary_search.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "iostream"
|
||||
using namespace std;
|
||||
|
||||
void binarySearch(int data_array[], int element, int len)
|
||||
{
|
||||
int low = 0;
|
||||
int high = len;
|
||||
while (low <= high)
|
||||
{
|
||||
int mid = (low + high)/2;
|
||||
int guess = data_array[mid];
|
||||
|
||||
if (guess == element)
|
||||
{
|
||||
cout<<"Element found at "<<mid<<" th index"<<endl ;
|
||||
return ;
|
||||
}
|
||||
else if (guess > element)
|
||||
{
|
||||
high = mid - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
cout<<"Element Not Found"<<endl ;
|
||||
return ; //number not found
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int data_array[] = {2,10,23,44,100,121};
|
||||
int length = sizeof(data_array) / sizeof(int);
|
||||
|
||||
binarySearch(data_array, 3, length) ; // not found case
|
||||
binarySearch(data_array, 2, length) ; // found at corner case
|
||||
binarySearch(data_array, 44, length) ; //found at middle case
|
||||
return 0;
|
||||
}
|
||||
|
||||
81
06_breadth-first_search/c++/01_graph_bfs.cpp
Normal file
81
06_breadth-first_search/c++/01_graph_bfs.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
template <typename T>
|
||||
class Graph {
|
||||
map <T , list<T>> adjList ;
|
||||
|
||||
public :
|
||||
Graph()
|
||||
{}
|
||||
|
||||
void addEdge(T u , T v , bool bidir = true)
|
||||
{
|
||||
adjList[u].push_back(v);
|
||||
if(bidir)
|
||||
adjList[v].push_back(u) ;
|
||||
}
|
||||
|
||||
void printAdjList()
|
||||
{
|
||||
for( auto key : adjList)
|
||||
{
|
||||
cout<<key.first<<"->" ;
|
||||
for(auto neighbours : key.second)
|
||||
cout<<neighbours<<"," ;
|
||||
|
||||
cout<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
void bfs(T src)
|
||||
{
|
||||
queue<T> q;
|
||||
|
||||
map<T , bool> visited ;
|
||||
|
||||
q.push(src) ;
|
||||
visited[src] = true ;
|
||||
|
||||
while(!q.empty())
|
||||
{
|
||||
T node = q.front() ;
|
||||
cout<<node<<" ," ;
|
||||
q.pop();
|
||||
|
||||
//push the neighbours
|
||||
|
||||
for(auto neighbours : adjList[node])
|
||||
{
|
||||
if(!visited[neighbours])
|
||||
{
|
||||
q.push(neighbours) ;
|
||||
visited[neighbours] = true ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} ;
|
||||
|
||||
int main() {
|
||||
Graph<int> g ;
|
||||
|
||||
//adding the edges in the Graph
|
||||
g.addEdge(0,1);
|
||||
g.addEdge(1,2);
|
||||
g.addEdge(0,4);
|
||||
g.addEdge(2,4);
|
||||
g.addEdge(2,3);
|
||||
g.addEdge(3,5);
|
||||
g.addEdge(3,4);
|
||||
|
||||
cout <<"The Graph is"<<endl;
|
||||
g.printAdjList();
|
||||
cout<<endl;
|
||||
|
||||
cout<<"The Breadth First Search from Node 0"<<endl;
|
||||
|
||||
g.bfs(0) ;
|
||||
}
|
||||
Reference in New Issue
Block a user