diff --git a/Algorithms/Graphs/breadth_first_search.cpp b/Algorithms/Graphs/breadth_first_search.cpp new file mode 100644 index 0000000..2a307b4 --- /dev/null +++ b/Algorithms/Graphs/breadth_first_search.cpp @@ -0,0 +1,63 @@ +//Graph Traversal Algorithm +//Language Used: C++ +//Breadth First Traversal algorithm to traverse all nodes of a graph considering only one component in graph +//Output: This algorithm will give nodes visited in breadth first manner and distance of each node from source. +//author:namanvats +#include +using namespace std; +#define MAX5 100010 +#define pb push_back +int dist[MAX5]; +bool visit[MAX5]; +vector vec[MAX5]; +queue q; +void bfs(int src) +{ + memset(visit,0,sizeof(visit)); + dist[src]=0; + q.push(src); + while(q.size()!=0) + { + int p=q.front(); + cout<>nodes>>edges; + cout<<"Enter edges between nodes"<<'\n'; + for(int i=0;i>x>>y; + vec[x].pb(y); + vec[y].pb(x); + } + cout<<"Enter Source node"<<'\n'; + int src; + cin>>src; + cout<<"The nodes are:"<<'\n'; + bfs(src); + cout<<'\n'; + cout<<"Distance of each node starting from source:"<<'\n'; + for(int i=1;i<=nodes;i++) + { + cout<<"Distance of node"<<" "< +using namespace std; + +// A iterative binary search function. It returns +// location of x in given array arr[l..r] if present, +// otherwise -1 +int binarySearch(int arr[], int l, int r, int x) +{ + while (l <= r) { + int m = l + (r - l) / 2; + + // Check if x is present at mid + if (arr[m] == x) + return m; + + // If x greater, ignore left half + if (arr[m] < x) + l = m + 1; + + // If x is smaller, ignore right half + else + r = m - 1; + } + + // if we reach here, then element was + // not present + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int x = 10; + int n = sizeof(arr) / sizeof(arr[0]); + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? cout << "Element is not present in array" + : cout << "Element is present at index " << result; + return 0; +} \ No newline at end of file diff --git a/Algorithms/Sorting Algorithms/merge_sort.cpp b/Algorithms/Sorting Algorithms/merge_sort.cpp new file mode 100644 index 0000000..006386a --- /dev/null +++ b/Algorithms/Sorting Algorithms/merge_sort.cpp @@ -0,0 +1,40 @@ +// Program for implementation of merge sort in c++ +// function msort() calls itself recurively while the function merge() merges two arrays + + +#include +using namespace std; + +void merge(int *a, int p,int q,int r){ + int left[q-p+2], right[r-q+1]; + left[q-p+1] = 99999, right[r-q] = 99999; + for(int l = 0; l<=q-p; l++) + left[l] = a[p+l]; + for(int k = 0; kright[k]){ + a[i] = right[k]; + k++; + } + else{ + a[i] = left[l]; + l++; + } + } +} +void msort(int *a, int p, int r){ + if(p +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +// A function to implement bubble sort +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n-1; i++) + + // Last i elements are already in place + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +// Driver code +int main() +{ + int arr[] = {64, 34, 25, 12, 22, 11, 90}; + int n = sizeof(arr)/sizeof(arr[0]); + bubbleSort(arr, n); + cout<<"Sorted array: \n"; + printArray(arr, n); + return 0; +} + +// This code is contributed by rathbhupendra diff --git a/Algorithms/Sorting/insertionsort.cpp b/Algorithms/Sorting/insertionsort.cpp new file mode 100644 index 0000000..5553d1c --- /dev/null +++ b/Algorithms/Sorting/insertionsort.cpp @@ -0,0 +1,47 @@ +// C++ program for insertion sort +#include +using namespace std; + +/* Function to sort an array using insertion sort*/ +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i - 1; + + /* Move elements of arr[0..i-1], that are + greater than key, to one position ahead + of their current position */ + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +// A utility function to print an array of size n +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + cout << arr[i] << " "; + cout << endl; +} + +/* Driver code */ +int main() +{ + int arr[] = { 12, 11, 13, 5, 6 }; + int n = sizeof(arr) / sizeof(arr[0]); + + insertionSort(arr, n); + printArray(arr, n); + + return 0; +} + +// This is code is contributed by rathbhupendra diff --git a/Algorithms/Sorting/selectionsort.cpp b/Algorithms/Sorting/selectionsort.cpp new file mode 100644 index 0000000..d808f52 --- /dev/null +++ b/Algorithms/Sorting/selectionsort.cpp @@ -0,0 +1,50 @@ +// C++ program for implementation of selection sort +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void selectionSort(int arr[], int n) +{ + int i, j, min_idx; + + // One by one move boundary of unsorted subarray + for (i = 0; i < n-1; i++) + { + // Find the minimum element in unsorted array + min_idx = i; + for (j = i+1; j < n; j++) + if (arr[j] < arr[min_idx]) + min_idx = j; + + // Swap the found minimum element with the first element + swap(&arr[min_idx], &arr[i]); + } +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +// Driver program to test above functions +int main() +{ + int arr[] = {64, 25, 12, 22, 11}; + int n = sizeof(arr)/sizeof(arr[0]); + selectionSort(arr, n); + cout << "Sorted array: \n"; + printArray(arr, n); + return 0; +} + +// This is code is contributed by rathbhupendra diff --git a/Kruskal's algorithm.cpp b/Kruskal's algorithm.cpp new file mode 100644 index 0000000..97b379f --- /dev/null +++ b/Kruskal's algorithm.cpp @@ -0,0 +1,58 @@ +// This program uses the Kruskal's algorithm to find out the minimum spanning tree for any connected weighted simple graph. + +#include + +using namespace std; + +int findset(int p[], int x){ + if(x!=p[x]){ + p[x]=findset(p,p[x]); + } + return p[x]; +} + +void unionset(int p[],int r[],int a,int b){ + int x=findset(p,a); + int y=findset(p,b); + if(x!=y){ + if(r[x]>n>>m; + int x,y,w; + vector>> E; + for(int i=0;i>x>>y>>w; + E.push_back({w,{x-1,y-1}}); + } + sort(E.begin(),E.end()); + int p[n],r[n]; + for(int i=0;i v; + for(int i=0;i