From 2c9de0ed94880946ef15ea4e0d3b9c86ea5ef775 Mon Sep 17 00:00:00 2001 From: Samruddhi Ghodake <72791227+samughodake@users.noreply.github.com> Date: Wed, 13 Oct 2021 18:16:40 +0530 Subject: [PATCH] chore(CPlusPlus): add recursion programs (#559) Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com> --- algorithms/CPlusPlus/README.md | 18 ++++++ .../CPlusPlus/Recursion/binary-search.cpp | 62 +++++++++++++++++++ .../Recursion/first-uppercase-letter.cpp | 52 ++++++++++++++++ .../Recursion/min-max-element-in-array.cpp | 57 +++++++++++++++++ .../CPlusPlus/Recursion/reverse-string.cpp | 48 ++++++++++++++ .../CPlusPlus/Recursion/string-length.cpp | 41 ++++++++++++ algorithms/CPlusPlus/Recursion/sum-of-n.cpp | 38 ++++++++++++ 7 files changed, 316 insertions(+) create mode 100644 algorithms/CPlusPlus/Recursion/binary-search.cpp create mode 100644 algorithms/CPlusPlus/Recursion/first-uppercase-letter.cpp create mode 100644 algorithms/CPlusPlus/Recursion/min-max-element-in-array.cpp create mode 100644 algorithms/CPlusPlus/Recursion/reverse-string.cpp create mode 100644 algorithms/CPlusPlus/Recursion/string-length.cpp create mode 100644 algorithms/CPlusPlus/Recursion/sum-of-n.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index cff090e3..beafdc97 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -1,6 +1,7 @@ # C++ ## Arrays + - [Counting Inversions](Arrays/counting-inversions.cpp) - [Dutch Flag Algorithm](Arrays/dutch-flag-algo.cpp) - [Left Rotation](Arrays/left-rotation.cpp) @@ -22,6 +23,7 @@ - [Smallest Possible Sum](Arrays/smallest-possible-sum.cpp) ## Dynamic-Programming + - [Longest Common Subsequence](Dynamic-Programming/longest-common-subsequence.cpp) - [Longest Common Substring](Dynamic-Programming/longest-common-substring.cpp) - [0/1-knapsack](Dynamic-Programming/01-knapsack.cpp) @@ -31,6 +33,7 @@ - [Coin Change](Dynamic-Programming/coin-change-problem.cpp) ## Graphs + - [Bellman Ford Algorithm](Graphs/bellman-ford.cpp) - [kruskal Algorithm](Graphs/kruskal-algorithm.cpp) - [Breadth First Search](Graphs/breadth-first-search.cpp) @@ -40,9 +43,11 @@ - [Connected Components](Graphs/total-connected-components.cpp) ## Multiplication + - [Karatsuba](Multiplication/karatsuba.cpp) ## Linked Lists + - [All possible insertions](Linked-Lists/all-possible-insertion.cpp) - [Singly linked lists](Linked-Lists/singly.cpp) - [doubley linked lists](Linked-Lists/doubly.cpp) @@ -57,6 +62,7 @@ - [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp) ## Searching + - [Linear Search](Searching/linear-search.cpp) - [Jump Search](Searching/jump-search.cpp) - [Binary Search](Searching/binary-search.cpp) @@ -67,12 +73,14 @@ - [Exponential Search](Searching/exponential-search.cpp) ## Stacks + - [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp) - [Reversing Stack](Stacks/reverse-stack.cpp) - [Stack using Array](Stacks/stack-using-array.cpp) - [Infix to postfix expression conversion](Stacks/infix-to-postfix.cpp) ## Sorting + - [Bubble Sort](Sorting/bubble-sort.cpp) - [Insertion Sort](Sorting/insertion-sort.cpp) - [Quicksort](Sorting/quick-sort.cpp) @@ -90,6 +98,7 @@ - [Cycle Sort](Sorting/cycle-sort.cpp) ## Strings + - [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp) - [All subsequence of a string (Recursion) ](Strings/sequence.cpp) - [String reversal](Strings/string-reverse.cpp) @@ -102,6 +111,7 @@ - [Boyer Moore pattern search](Strings/Boyer_Moore.cpp) ## Trees + - [Creating Binary Tree](Trees/build-binary-tree.cpp) - [Counting and finding sum of all the nodes in BST](Trees/count-and-sum-of-nodes-in-binary-tree.cpp) - [Level Order Traversal](Trees/level-order-traversal.cpp) @@ -118,6 +128,7 @@ - [Fenwick Tree](Trees/Fenwick_Tree.cpp) # Maths + - [Kaprekar Number](Maths/Kaprekar-number.cpp) - [Prime Number](Maths/prime-check.cpp) - [Prime Sieve](Maths/prime-sieve.cpp) @@ -131,6 +142,7 @@ - [Prime-number](Maths/prime-number.cpp) # Recursion + - [Tower of Hanoi](Recursion/towerofHanoi.cpp) - [Factorial](Recursion/factorial.cpp) - [Permutation](Recursion/permutation.cpp) @@ -138,4 +150,10 @@ - [Sum of all elements of an array](Recursion/Sum-of-all-elements-in-an-array.cpp) - [Decimal number to Binary conversion](Recursion/decimal-to-binary-conversion.cpp) - [Sum of digits of a decimal integer](Recursion/sum-of-digits-of-an-integer.cpp) +- [Binary search using recursion](Recursion/binary-search.cpp) +- [First uppercase letter in a string](Recursion/first-uppercase-letter.cpp) +- [Reverse a string using recursion](Recursion/reverse-string.cpp) +- [Find string length using recursion](Recursion/string-length.cpp) +- [Sum of n natural numbers](Recursion/sum-of-n.cpp) +- [Minimum and maximum element in array](Recursion/min-max-element-in-array.cpp) - [Prime Check](Recursion/recursive-prime.cpp) diff --git a/algorithms/CPlusPlus/Recursion/binary-search.cpp b/algorithms/CPlusPlus/Recursion/binary-search.cpp new file mode 100644 index 00000000..7fd808bb --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/binary-search.cpp @@ -0,0 +1,62 @@ +/* +Description: Binary search using recursion + +Approach: To use recursion, for updating start and end variables. + +Time Complexity: O(log n) +*/ + +#include +using namespace std; + +//function starts +int search(int arr[], int n, int start, int end, int target) +{ + //base case + //if start is greater than end, it means we haven't found our target element + //return -1 + if (start > end) + { + return -1; + } + //calculating mind element + int mid = start + (end - start) / 2; + + if (arr[mid] == target) + { + return mid; + } + if (arr[mid] > target) + { + //end = mid-1; + return search(arr, n, start, mid - 1, target); + } + else + { + //start=mid+1; + return search(arr, n, mid + 1, end, target); + } +} + +//main starts +int main() +{ + // Write C++ code here + int arr[] = {1, 2, 3, 4, 5, 6}; + int n = sizeof(arr) / sizeof(arr[0]); + int target = 6; + int start = 0; + int end = n - 1; + int ind = search(arr, n, start, end, target); + cout << "Element found at index: " << ind; + return 0; +} + +/* +Sample input: +arr=[1,2,3,4,5,6] +target=6; + +Output: +Element found at index: 5 +*/ \ No newline at end of file diff --git a/algorithms/CPlusPlus/Recursion/first-uppercase-letter.cpp b/algorithms/CPlusPlus/Recursion/first-uppercase-letter.cpp new file mode 100644 index 00000000..2be87827 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/first-uppercase-letter.cpp @@ -0,0 +1,52 @@ +/* +Description: Program to print the first uppercase letter in a string + +Approach: Use a varialbe to iterate over the string +Increment the iterator by 1 at every recursive call +If the value of iterator reaches till the length of the string, return '\0 + +Time Complexity: O(n) where n is the length of the string +*/ + +#include +using namespace std; + +//function starts +char upper(string s, int i, int len) +{ + //base case + //if the value of i reaches till length of the string + //that means we have not found any uppercase letter + //return '\0 + if (i == len) + { + return '\0'; + } + //if uppercase letter found + //return it + if (s[i] >= 'A' and s[i] <= 'Z') + { + return s[i]; + } + //recursive function call + //the iterator i starts from 0 and increments by 1 at each function call + return upper(s, i + 1, len); +} + +//main starts +int main() +{ + string s = "helloalL"; + int len = s.size(); + char c = upper(s, 0, len); + cout << c; + return 0; +} + +/* +Sample Input: helloalL +Output: L + +Sample Input: hELLOaLL +Output: E +*/ \ No newline at end of file diff --git a/algorithms/CPlusPlus/Recursion/min-max-element-in-array.cpp b/algorithms/CPlusPlus/Recursion/min-max-element-in-array.cpp new file mode 100644 index 00000000..33b25678 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/min-max-element-in-array.cpp @@ -0,0 +1,57 @@ +/* +Description: To find minimum and maximum element in array using recursion + +Time Complexity: O(n) +*/ + +#include +using namespace std; + +//function to find minimum element in the array +int findmin(int arr[], int n) +{ + //base case + //if there is only 1 element in array, it will be the minimum element + if (n == 1) + { + return arr[0]; + } + //recursive function call + int mm = findmin(arr, n - 1); + //return minimum element from every recursive call to the function + return min(arr[n - 1], mm); +} + +//function to find maximum element in the array +int findmax(int arr[], int n) +{ + //base case + //if there is only 1 element in array, it will be the maximum element + if (n == 1) + { + return arr[0]; + } + //recursive function call + int mm = findmax(arr, n - 1); + //return maximum element from every recursive call to the function + return max(arr[n - 1], mm); +} + +//main starts +int main() +{ + int arr[] = {-1, -2, 3, 4, 5, 6}; + int n = 6; + cout << "Minimum element in array: " << findmin(arr, n) << endl; + cout << "Maximum element in array: " << findmax(arr, n); + return 0; +} + +/* +Sample Input: +arr=[-1,-2,3,4,5,6] + +Output: +Minimum element in array: -2 +Maximum element in array: 6 +*/ \ No newline at end of file diff --git a/algorithms/CPlusPlus/Recursion/reverse-string.cpp b/algorithms/CPlusPlus/Recursion/reverse-string.cpp new file mode 100644 index 00000000..33ea7ab6 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/reverse-string.cpp @@ -0,0 +1,48 @@ +/* +Description: Program to reverse string using recursion + +Approach: To maintain start and end variables in string. +Start represents starting index and end represent ending index. +At each iteration, the character at starting index will be swapped +with the character at ending index. +At every recursive call we will update start and end variables. +Start will be incremented by 1 and end will be decremented by 1. +We will return string when the value of start will be greater than end. +This indicated the string has been reversed. + +Time complexity: O(n) where n is the length of string +*/ + +#include +using namespace std; + +//function starts +string rev(string s, int start, int end) +{ + //base case + if (start > end) + { + return s; + } + //swapping characters at start and end index + swap(s[start], s[end]); + + //recursive call with incrementing start and decrementing end + return rev(s, start + 1, end - 1); +} + +//main starts +int main() +{ + // Write C++ code here + string s = "hello"; + int len = s.size(); + cout << endl; + cout << rev(s, 0, len - 1); + return 0; +} + +/* +Input: hello +Output: olleh +*/ \ No newline at end of file diff --git a/algorithms/CPlusPlus/Recursion/string-length.cpp b/algorithms/CPlusPlus/Recursion/string-length.cpp new file mode 100644 index 00000000..69278e37 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/string-length.cpp @@ -0,0 +1,41 @@ +/* +Description: find length of string using recursion. + +Approach: To run the recursive call till the end of the string. +Maintaining a iterator variable which will increment at every recursive call +If we reach at the end of the string, return the value of the iterator variable +which will be the length of the string. + +Time Complexity: O(n) where n is the length of the string +*/ +#include +using namespace std; + +//function starts +int cal_length(string s, int i = 0) +{ + if (s[i] == '\0') + { + return i; + } + return cal_length(s, i + 1); +} + +//main starts +int main() +{ + string s = "hello"; + cout << cal_length(s); + return 0; +} + +/* +Sample Input: hello +Output: 5 + +Sample Input: good +Output: 4 + +Sample Input: github +Output: 6 +*/ diff --git a/algorithms/CPlusPlus/Recursion/sum-of-n.cpp b/algorithms/CPlusPlus/Recursion/sum-of-n.cpp new file mode 100644 index 00000000..6ee35125 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/sum-of-n.cpp @@ -0,0 +1,38 @@ +/* +Description: A program to find sum of n natural numbers using recursion. + +Time Complexity: O(n) +*/ + +#include +using namespace std; + +//function starts +int sumofN(int n) +{ + //base case + if (n <= 1) + { + return n; + } + //recursive function call + int res = sumofN(n - 1); + //return sum + return n + res; +} + +// Driver code +int main() +{ + int n = 5; + cout << sumofN(n); + return 0; +} + +/* +Sample Input: 5 +Output: 15 + +Sample Input: 3 +Output: 6 +*/ \ No newline at end of file