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; + +int binary_search(int *ar, int low, int high, int key) +{ + if(high < low) + return -1; + + int mid = low + (high - low) / 2; + if(key == ar[mid]) + return mid; + else if(key < ar[mid]) + return binary_search(ar, low, mid - 1, key); + else if(key > ar[mid]) + return binary_search(ar, mid + 1, high, key); + +} + +int main() +{ + int n, key; + cout << "Enter the size of array: "; + cin >> n; + int ar[n]; + cout << "Enter the elements: " << endl; + for(int i = 0; i < n; i++) + cin >> ar[i]; + + cout << "Enter the value to be searched: "; + cin >> key; + + int result = binary_search(ar, 0, n-1, key); + + if(result == -1) + cout << "The key is not present in the array" << endl; + else + cout << "The key is present in array at index: " << result << endl; + + +} \ No newline at end of file diff --git a/Algorithms/Searching/linear_searching.cpp b/Algorithms/Searching/linear_searching.cpp new file mode 100644 index 0000000..cda9db2 --- /dev/null +++ b/Algorithms/Searching/linear_searching.cpp @@ -0,0 +1,42 @@ +// Program to search in an array using Linear Search +// Language used: C++ +// Author: ExpressHermes + + +#include +using namespace std; + +int linear_search(int *ar, int n, int key) +{ + int found = 0, i; + for(i = 0; i < n - 1; i++) + { + if(ar[i] == key) + { + found = 1; + break; + } + } + + if(found == 1) + cout << "The key is present in the array at index: " << i << endl; + else + cout << "The key is not present in the array" << endl; +} + +int main() +{ + int n, key; + cout << "Enter the size of array: "; + cin >> n; + int ar[n]; + cout << "Enter the elements: " << endl; + for(int i = 0; i < n; i++) + cin >> ar[i]; + + cout << "Enter the value to be searched: "; + cin >> key; + + linear_search(ar, n, key); + +} \ No newline at end of file diff --git a/Algorithms/Sorting/bubble_sort.cpp b/Algorithms/Sorting/bubble_sort.cpp new file mode 100644 index 0000000..cf14499 --- /dev/null +++ b/Algorithms/Sorting/bubble_sort.cpp @@ -0,0 +1,47 @@ +// Program to sort an array in ascending order using Binary Sorting +// Language used: C++ +// Description: In binary sort, we constantly swap the adjacent elements in wrong order +// Author: ExpressHermes + +#include +using namespace std; + +void swap(int *a, int *b) +{ + int temp = *b; + *b = *a; + *a = temp; +} + +void bubble_sort(int *ar, int n) +{ + for(int i = 0; i < n; i++) + { + for(int j = 0; j < n-i-1; j++) + { + if(ar[j] > ar[j+1]) + { + // swap elements in the wrong order + swap(&ar[j], &ar[j+1]); + } + } + + } +} + +int main() +{ + int n; + cout << "Enter the number of elements: " << endl; + cin >> n; + int ar[n]; + cout << "Enter the elements of array: " << endl; + for(int i = 0; i < n; i++) + cin >> ar[i]; + + bubble_sort(ar, n); + + for(int i = 0; i < n; i++) + cout << ar[i] << " "; + cout << endl; +} \ No newline at end of file diff --git a/Algorithms/Sorting/insertion_sort.cpp b/Algorithms/Sorting/insertion_sort.cpp new file mode 100644 index 0000000..8774b6a --- /dev/null +++ b/Algorithms/Sorting/insertion_sort.cpp @@ -0,0 +1,41 @@ +// Program to sort an array in ascending order using Insertion Sorting +// Language used: C++ +// Description: In insertion sort, we pick an element a[i] and we +// insert into it's right position in the sorted subarray ar[0] and ar[i-1] +// Author: ExpressHermes + +#include +using namespace std; + +void insertion_sort(int *ar, int n) +{ + for (int i = 1; i < n; i++) + { + int value = ar[i]; + int j = i - 1; + + while (j >= 0 && ar[j] > value) + { + ar[j + 1] = ar[j]; + j = j - 1; + } + ar[j + 1] = value; + } +} + +int main() +{ + int n; + cout << "Enter the number of elements: " << endl; + cin >> n; + int ar[n]; + cout << "Enter the elements of array: " << endl; + for(int i = 0; i < n; i++) + cin >> ar[i]; + + insertion_sort(ar, n); + + for(int i = 0; i < n; i++) + cout << ar[i] << " "; + cout << endl; +} \ No newline at end of file diff --git a/Algorithms/Sorting/quick_sort.cpp b/Algorithms/Sorting/quick_sort.cpp new file mode 100644 index 0000000..887984d --- /dev/null +++ b/Algorithms/Sorting/quick_sort.cpp @@ -0,0 +1,51 @@ +// Program to sort an array in descending order using Quick Sort +// Language used: C++ +// Description: Quick Sort works on Divide and Conquer algorithm. In this we take a pivot and partition the +// whole array around this pivot. We put the pivot at correct elements at correct position and +// put all the smaller elements(smaller than pivot) after the pivot and all the greater elements +// before the pivot. +// Author: ExpressHermes + +#include +using namespace std; + +void quick_sort(int *ar, int n) +{ + if(n < 2) + return; + + int pivot = ar[n/2]; + int i, j; + for(i = 0, j = n - 1; ;i++, j--) + { + while(ar[i] > ar[pivot]) i++; + while(ar[j] < ar[pivot]) j--; + + if(i >= j) + break; + + int temp = ar[i]; + ar[i] = ar[j]; + ar[j] = temp; + } + + quick_sort(ar, i); + quick_sort(ar + i, n - i); +} + +int main() +{ + int n; + cout << "Enter the number of elements: " << endl; + cin >> n; + int ar[n]; + cout << "Enter the elements of array: " << endl; + for(int i = 0; i < n; i++) + cin >> ar[i]; + + quick_sort(ar, n); + + for(int i = 0; i < n; i++) + cout << ar[i] << " "; + cout << endl; +} \ No newline at end of file diff --git a/Algorithms/Sorting/selection_sort.cpp b/Algorithms/Sorting/selection_sort.cpp new file mode 100644 index 0000000..4a7fa7a --- /dev/null +++ b/Algorithms/Sorting/selection_sort.cpp @@ -0,0 +1,48 @@ +// Program to sort an array in ascending order using Selection Sorting +// Language used: C++ +// Description: In every iteration of selection sort, the minimum element (considering ascending order) +// from the unsorted subarray is picked and moved to the sorted subarray. +// Author: ExpressHermes + +#include +using namespace std; + +void swap(int *a, int *b) +{ + int temp = *b; + *b = *a; + *a = temp; +} + +void selection_sort(int *ar, int n) +{ + for(int i = 0; i < n; i++) + { + int min_index = i; + for(int j = i + 1; j < n; j++) + { + if(ar[j] < ar[min_index]) + min_index = j; + } + + // place the element in it's right position + swap(&ar[i], &ar[min_index]); + } +} + +int main() +{ + int n; + cout << "Enter the number of elements: " << endl; + cin >> n; + int ar[n]; + cout << "Enter the elements of array: " << endl; + for(int i = 0; i < n; i++) + cin >> ar[i]; + + selection_sort(ar, n); + + for(int i = 0; i < n; i++) + cout << ar[i] << " "; + cout << endl; +} \ No newline at end of file diff --git a/README.md b/README.md index fcf7fb8..6f5939c 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,6 @@ This is a library of various algorithms that are used in competitive programming ### sample algorithm [disjoint_set_union.cpp](Algorithms/disjoint_set_union/disjoint_set_union.cpp) + + +READ instructions before making any pull request.