Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions binary-search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

//Binary search .................Iterative method best for sorted array

# include<bits/stdc++.h>
using namespace std; //std::cout or std::cin

int main(){
int n,t;
cout<<"enter test cases"<<endl;
cin>>t;
//cout<<(sizeof(arr)/sizeof(arr[0])); sizeof(arr) = datatype of arr * number of variable
while(t--){
cout<<"enter size of array "<<endl;
cin>>n;
int arr[n];
cout<<"Enter array element\n";

for(int i=0;i<n;i++){
cin>>arr[i];
}

int key;
cout<<"enter key to search"<<endl;
cin>>key;

int low=0,high=n-1, flag=0;
for(int i=0;i<n;i++){
// int mid =(low +high)/2; //It take more time 5.2 second
// cout<<mid;
int mid=low+(high-low)/2; // It take less time 4.1 second
if(arr[mid]==key){
cout<<"present"<<endl;
flag=1;
break;
}
else if(arr[mid]>key){
high= mid-1;
}
else if(arr[mid] <key)
low = mid+1;
}

if(flag==0){
cout<<"Not present"<<endl;
}
}
return 0;
}
60 changes: 60 additions & 0 deletions floyd_Warshall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Floyd Warshall algorithm
// Use to find all pairs of shortest path
#include<bits/stdc++.h>
using namespace std;

#define V 4 // Number of vertices in graph problem

#define INF INT_MAX // INF infinite maximum distance value for not connected vertices <limits.h> header file used

void output(int dist[][V]) // output matrix show here
{

cout<<"ALL SHORTEST PATH FOR EVERY VERTICES\n";


for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(dist[i][j]==INF)
cout<<"INF"<<" ";
else
cout<<dist[i][j]<<" ";
}
cout<<endl;
}
}
void flyodWarshall(int g[][V]) // g graph to find all pairs shortest path
{
int dist[V][V]; // 2D array to contain final all pairs path


for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
dist[i][j]=g[i][j]; // here we are just putting the original graph values to dist[][] matrix
}
}

for(int k=0;k<V;k++){ // select source vertices

for(int i=0;i<V;i++){ // select destination vertices

for(int j=0;j<V;j++){ // if k shortest path it will work

if((dist[i][j]>dist[i][k]+dist[k][j]) && (dist[i][k]!=INF) &&(dist[k][j]!=INF))
dist[i][j]=dist[i][k]+dist[k][j];

}
}
}
output(dist); // print shortest matrix
}

int main(){

int g[V][V]={{0,3,INF,7},{8,0,2,INF},{5,INF,0,1},{2,INF,INF,0}}; // index value 0 show self loop edges and INF infinite largest distance


flyodWarshall(g); // use to find paths

return 0;
}