From 7c9f3ac9f27ed2195e0e14ce9c997d207745880c Mon Sep 17 00:00:00 2001 From: Kavya Date: Tue, 2 Oct 2018 16:31:37 +0530 Subject: [PATCH 1/9] Added String algorithm --- Strings/Rabin_carp_algo.cpp | 117 ++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 Strings/Rabin_carp_algo.cpp diff --git a/Strings/Rabin_carp_algo.cpp b/Strings/Rabin_carp_algo.cpp new file mode 100644 index 00000000..acd6fd15 --- /dev/null +++ b/Strings/Rabin_carp_algo.cpp @@ -0,0 +1,117 @@ + /* + + * C++ Program to Implement Rabin-Karp Algorithm + + */ + + #include + + #include + + #include + + #include + + using namespace std; + + #define d 256 + + /* + + * search a substring in a string + + */ + + void search(char *pat, char *txt, int q) + + { + + int M = strlen(pat); + + int N = strlen(txt); + + int i, j; + + int p = 0; + + int t = 0; + + int h = 1; + + for (i = 0; i < M - 1; i++) + + h = (h * d) % q; + + for (i = 0; i < M; i++) + + { + + p = (d *p + pat[i]) % q; + + t = (d * t + txt[i]) % q; + + } + + for (i = 0; i <= N - M; i++) + + { + + if (p == t) + + { + + for (j = 0; j < M; j++) + + { + + if (txt[i + j] != pat[j]) + + break; + + } + + if (j == M) + + { + + cout<<"Pattern found at index: "< Date: Tue, 2 Oct 2018 17:20:13 +0530 Subject: [PATCH 2/9] added path to Strings in ReadMe file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6886d0f2..14d9f2fa 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ - [Hashes](hashes) - [Searches](searches) - [Sorting](sorting) -- [Strings](strings) +- [Strings](https://github.com/kavya98527/cpp/tree/master/Strings) - [Traversals](traversals) ## License From 3ae61a30e0720033d9722dc56713ba97d9f41576 Mon Sep 17 00:00:00 2001 From: Kavya Date: Tue, 2 Oct 2018 17:36:11 +0530 Subject: [PATCH 3/9] added path to Strings in ReadMe file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 14d9f2fa..178ab926 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ - [Hashes](hashes) - [Searches](searches) - [Sorting](sorting) -- [Strings](https://github.com/kavya98527/cpp/tree/master/Strings) +- [Strings](https://github.com/AllAlgorithms/cpp/tree/master/Strings) - [Traversals](traversals) ## License From 4250b364d9adc347d717b03c30800717f82f346e Mon Sep 17 00:00:00 2001 From: Kavya Date: Wed, 3 Oct 2018 22:07:33 +0530 Subject: [PATCH 4/9] added a new String algorithm --- Strings/z algo.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Strings/z algo.cpp diff --git a/Strings/z algo.cpp b/Strings/z algo.cpp new file mode 100644 index 00000000..1aabaff9 --- /dev/null +++ b/Strings/z algo.cpp @@ -0,0 +1,115 @@ + /* + + * C++ Program to Implement Z-Algorithm + + */ + + #include + + #include + + #include + + using namespace std; + + bool zAlgorithm(string pattern, string target) + + { + + string s = pattern + '$' + target; + + int n = s.length(); + + vector z(n, 0); + + int goal = pattern.length(); + + int r = 0, l = 0, i; + + for (int k = 1; k < n; k++) + + { + + if (k > r) + + { + + for (i = k; i < n && s[i] == s[i - k]; i++); + + if (i > k) + + { + + z[k] = i - k; + + l = k; + + r = i - 1; + + } + + } + + else + + { + + int kt = k - l, b = r - k + 1; + + if (z[kt] > b) + + { + + for (i = r + 1; i < n && s[i] == s[i - k]; i++); + + z[k] = i - k; + + l = k; + + r = i - 1; + + } + + } + + if (z[k] == goal) + + return true; + + } + + return false; + + } + + + + int main() + + { + + string tar = "This is a sample Testcase"; + + string pat = "case"; + + if (zAlgorithm(pat, tar)) + + cout<<"'"< Date: Wed, 3 Oct 2018 23:11:15 +0530 Subject: [PATCH 5/9] added shaker sort.cpp --- sorting/shaker_sort.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sorting/shaker_sort.cpp diff --git a/sorting/shaker_sort.cpp b/sorting/shaker_sort.cpp new file mode 100644 index 00000000..f65f3d8e --- /dev/null +++ b/sorting/shaker_sort.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +// A function to swap values using call by reference. +void swap(int *a, int *b) +{ + int temp; + temp = *a; + *a = *b; + *b = temp; +} + +// A function implementing shaker sort. +void ShakerSort(int a[], int n) +{ + int i, j, k; + for(i = 0; i < n;) + { + // First phase for ascending highest value to the highest unsorted index. + for(j = i+1; j < n; j++) + { + if(a[j] < a[j-1]) + swap(&a[j], &a[j-1]); + } + // Decrementing highest index. + n--; + + // Second phase for descending lowest value to the lowest unsorted index. + for(k = n-1; k > i; k--) + { + if(a[k] < a[k-1]) + swap(&a[k], &a[k-1]); + } + // Incrementing lowest index. + i++; + } +} + +int main() +{ + int n, i; + cout<<"\nEnter the number of data element to be sorted: "; + cin>>n; + + int arr[n]; + for(i = 0; i < n; i++) + { + cout<<"Enter element "<>arr[i]; + } + + ShakerSort(arr, n); + + // Printing the sorted data. + cout<<"\nSorted Data "; + for (i = 0; i < n; i++) + cout<<"->"< Date: Wed, 3 Oct 2018 23:45:08 +0530 Subject: [PATCH 6/9] added stooge sort.cpp --- sorting/stooge_sort.cpp | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 sorting/stooge_sort.cpp diff --git a/sorting/stooge_sort.cpp b/sorting/stooge_sort.cpp new file mode 100644 index 00000000..5fd219c9 --- /dev/null +++ b/sorting/stooge_sort.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +// A function implementing stooge sort. +void StoogeSort(int a[],int start, int end) +{ + int temp; + // Further breaking the array if the Subpart's length is more than 2. + if(end-start+1 > 2) + { + temp = (end-start+1)/3; + StoogeSort(a, start, end-temp); + StoogeSort(a, start+temp, end); + StoogeSort(a, start, end-temp); + } + + // swapping the element at start and end. + if(a[end] < a[start]) + { + temp = a[start]; + a[start] = a[end]; + a[end] = temp; + } +} + +int main() +{ + int n, i; + cout<<"\nEnter the number of data element to be sorted: "; + cin>>n; + + int arr[n]; + for(i = 0; i < n; i++) + { + cout<<"Enter element "<>arr[i]; + } + + StoogeSort(arr, 0, n-1); + + // Printing the sorted data. + cout<<"\nSorted Data "; + for (i = 0; i < n; i++) + cout<<"->"< Date: Wed, 30 Jan 2019 13:21:22 -0500 Subject: [PATCH 7/9] Update and rename README.md to readme.md --- README.md | 46 ------------------ readme.md | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 46 deletions(-) delete mode 100644 README.md create mode 100644 readme.md diff --git a/README.md b/README.md deleted file mode 100644 index 178ab926..00000000 --- a/README.md +++ /dev/null @@ -1,46 +0,0 @@ -
- C Plus Plus Logo -
-
- -
-
-

All â–²lgorithms implemented in C Plus Plus

- - - - - -
-
- algorithms.abranhe.com -
- - -## Contents - -- [Arithmetic Analysis](arithmetic-analysis) -- [File Transfer Protocol](file-transfer-protocol) -- [Greedy Algorithms](greedy-algorithms) -- [Graphs](graphs) -- [Math](math) -- [Neutral Network](neutral-network) -- [Ciphers](ciphers) -- [Data Structures](data-structures) -- [Dynamic Programming](dynamic-programming) -- [Hashes](hashes) -- [Searches](searches) -- [Sorting](sorting) -- [Strings](https://github.com/AllAlgorithms/cpp/tree/master/Strings) -- [Traversals](traversals) - -## License - -This work is licensed under a [MIT License](https://github.com/abranhe/algorithms/blob/master/LICENSE) - -[![MIT IMG][mit-license]]((https://github.com/abranhe/algorithms/blob/master/LICENSE)) - -To the extent possible under law, [Carlos Abraham](https://go.abranhe.com/github) has waived all copyright and related or neighboring rights to this work. - - -[mit-license]: https://cdn.abraham.gq/projects/algorithms/mit-license.png diff --git a/readme.md b/readme.md new file mode 100644 index 00000000..5501e373 --- /dev/null +++ b/readme.md @@ -0,0 +1,139 @@ +
+
+
+
+
+ +
+
+
+
+
+
+ +
+
+

All â–²lgorithms implemented in C++

+ + + + + +
+
+ allalgorithms.com +
+ + +## Contents + +- [Artificial Intelligence](#artificial-intelligence) +- [Backtracking](#backtracking) +- [Bit Manipulation](#bit-manipulation) +- [Cellular Automaton](#cellular-automaton) +- [Ciphers](#ciphers) +- [Computational Geometry](#computational-geometry) +- [Cryptography](#cryptography) +- [Data Structures](#data-structures) +- [Divide and conquer](#divide-and-conquer) +- [Dynamic Programming](#dynamic-programming) +- [Gaming Theory](#gaming-theory) +- [Graphs](#graphs) +- [Greedy Algorithms](#greedy-algorithms) +- [Math](#math) +- [Networking](#networking) +- [Numerical Analysis](#numerical-analysis) +- [Operating system](#operating-system) +- [Randomized Algorithms](#randomized-algorithms) +- [Searches](#searches) +- [Selections Algorithms](#selections-algorithms) +- [Sorting](#sorting) +- [Strings](#strings) +- [Online Challenges](#online-challenges) +- [Others](#others) + +## Data Structures + +- Queue + - [Circular Buffer](data-structures/queue/circular_buffer.cpp) + - [Queue](data-structures/queue/queue.cpp) +- Stack + - [Stack](data-structures/stack/stack.cpp) + +## Dynamic Programming + +- [Coin Change](dynamic-programming/coin_change.cpp) +- [Edit Distance](dynamic-programming/edit_distance.cpp) +- [Knapsack](dynamic-programming/knapsack.cpp) +- [Longest common subsequence](dynamic-programming/lcs.cpp) +- [Longest Path](dynamic-programming/longest_path.cpp) +- [Ways to Cover](dynamic-programming/ways_to_cover.cpp) + +## Graphs + +- [Bellman Ford](graphs/bellman_ford.cpp) +- [Breadth-first search](graphs/bfs.cpp) +- [Count Disconnected Components](graphs/count_disconnected_components.cpp) +- [Depth-first search](graphs/dfs.cpp) +- [Dijkstra](graphs/dijkstra.cpp) +- [Floyed Warshall](graphs/floyd_warshall.cpp) +- [Prims Adjacency List](graphs/prims_adjacency_list.cpp) + +## Math + +- [Collatz](math/collatz.cpp) +- [Euclids Greatest common divisor](math/euclids_gcd.cpp) +- [Factorial](math/factorial.cpp) +- [Greatest common divisor of array](math/gcd_of_array.cpp) +- [Least common multiple of array](math/lcm_of_array.cpp) +- [Lucky Numbers](math/lucky_numbers.cpp) +- [Modular Exponentiations](math/modular_exponentiations.cpp) +- [nth Fibonacci Number using Goldenratio](math/nth_fibonacci_using_goldenratio.cpp) + +## Searches + +- [Binary Search](searches/binary_search.cpp) +- [Jump Search](searches/jump_search.cpp) +- [Linear Search](searches/linear_search.cpp) +- [Ternary Search](searches/ternary_search.cpp) + +## Sorting + +- [Bubble Sort](sorting/bubble_sort.cpp) +- [Heap Sort](sorting/heap_sort.cpp) +- [Heap Sort without vectors](sorting/heap_sort_without_vectors.cpp) +- [Insertion Sort](sorting/insertion_sort.cpp) +- [Merge Sort](sorting/merge_sort.cpp) +- [Quick Sort](sorting/quick_sort.cpp) +- [Selection Sort](sorting/selection_sort.cpp) +- [Sort Vector](sorting/sort_vector.cpp) +- [Tree Sort](sorting/tree_sort.cpp) + +## Strings + +- [Anagram Check](strings/anagram_check.cpp) +- [Lexicographic Ranking](strings/lexicographic_ranking.cpp) +- [Longest Palindrome Subset](strings/longest_palindrome_subset.cpp) +- [Naive Search](strings/naive_search.cpp) +- [Permutations of string](strings/permutations_of_string.cpp) +- [Print duplicate string](strings/print_duplicate_string.cpp) +- [Rabin Karp](strings/rabin_karp.cpp) +- [Remove Adjacent Duplicates](strings/remove_adjacent_duplicates.cpp) +- [Remove Duplicates](strings/remove_duplicates.cpp) +- [Reverse String](strings/reverse_string.cpp) + +## License + +This work is released under [MIT License](https://github.com/abranhe/algorithms/blob/master/liceense) + +[![MIT IMG](https://cdn.abranhe.com/projects/algorithms/mit-license.png)](https://github.com/abranhe/algorithms/blob/master/license) + +To the extent possible under law, [Carlos Abraham](https://go.abranhe.com/github) has waived all copyright and related or neighboring rights to this work. + + +
+ + + +
+
From 6e28c7506dc3e6bd0e4a92c765de8338acc5cece Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 30 Jan 2019 13:21:58 -0500 Subject: [PATCH 8/9] Rename Strings/Rabin_carp_algo.cpp to strings/rabin_carp.cpp --- Strings/Rabin_carp_algo.cpp => strings/rabin_carp.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Strings/Rabin_carp_algo.cpp => strings/rabin_carp.cpp (100%) diff --git a/Strings/Rabin_carp_algo.cpp b/strings/rabin_carp.cpp similarity index 100% rename from Strings/Rabin_carp_algo.cpp rename to strings/rabin_carp.cpp From 20e061278cb3407ab876d0b37bfbfb3463b7d828 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 30 Jan 2019 13:22:58 -0500 Subject: [PATCH 9/9] Update and rename Strings/z algo.cpp to strings/z_algorithm.cpp --- Strings/z algo.cpp => strings/z_algorithm.cpp | 4 ---- 1 file changed, 4 deletions(-) rename Strings/z algo.cpp => strings/z_algorithm.cpp (99%) diff --git a/Strings/z algo.cpp b/strings/z_algorithm.cpp similarity index 99% rename from Strings/z algo.cpp rename to strings/z_algorithm.cpp index 1aabaff9..ab2468c8 100644 --- a/Strings/z algo.cpp +++ b/strings/z_algorithm.cpp @@ -1,13 +1,9 @@ /* - * C++ Program to Implement Z-Algorithm - */ #include - #include - #include using namespace std;