Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 3 recusrion problem: sum of array element, linear and binary search (Issue: #296) #298

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
92 changes: 92 additions & 0 deletions C++/binary_search_using_recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Binary Search using Recursion

#include <iostream>
using namespace std;

int binarySearch(int arr[], int low, int high, int key)
{
if (low > high) // Base case: If the element is not found
return -1;
int mid = low + (high - low) / 2; // Calculate the middle index
if (arr[mid] == key) // If the element is found at the middle index
return mid;
if (arr[mid] > key) // If the key is smaller, search in the left half
return binarySearch(arr, low, mid - 1, key);
return binarySearch(arr, mid + 1, high, key); // Search in the right half
}

int main()
{
int n, key;
cout << "Enter the number of elements in the array: ";
cin >> n; // Input the number of elements in the array
int arr[n];
cout << "Enter the elements of the array in sorted order: ";
for (int i = 0; i < n; i++)
cin >> arr[i]; // Input the elements of the array
cout << "Enter the element to search: ";
cin >> key; // Input the element to search
int index = binarySearch(arr, 0, n - 1, key); // Function call
if (index != -1)
cout << "Element found at index: " << index << endl; // Output if element is found
else
cout << "Element not found" << endl; // Output if element is not found
return 0;
}


/*
**Explanation:**

1. Function Declaration (binarySearch)

The function binarySearch is declared with four parameters:
arr[]: the array containing the elements.
low: the starting index of the array.
high: the ending index of the array.
key: the element to search in the array.

2. Base Case (if (low > high))

The base case checks if the starting index (low) is greater than the ending index (high). If low becomes greater than high, it means the element is not found in the array, and the function returns -1.

3. Calculate the Middle Index

The middle index (mid) is calculated as the average of the low and high indices to divide the array into two halves.

4. Check if the Element is Found

If the element is found at the middle index, the function returns the index.

5. Recursive Calls

If the element is not found at the middle index, the function makes a recursive call to search in the left half if the key is smaller than the middle element (arr[mid]). Otherwise, it searches in the right half.

6. Main Function

Input:
The program first asks the user for the number of elements (n) in the array.
Then, the user is prompted to enter the elements of the array in sorted order.
The user is also asked to enter the element (key) to search in the array.

Output:
The binarySearch function is called with the entire array, the starting index (0), the ending index (n - 1), and the element to search (key).
If the element is found, the function returns the index where the element is found, and the program outputs "Element found at index: <index>".
If the element is not found, the function returns -1, and the program outputs "Element not found".


Example Walkthrough:
If the input is:

n = 5
arr = [2, 4, 6, 8, 10]
key = 8

The recursive process looks like this:

binarySearch(arr, 0, 4, 8) = binarySearch(arr, 3, 4, 8)
binarySearch(arr, 3, 4, 8) = binarySearch(arr, 3, 3, 8)

Finally, the element 8 is found at index 3.

*/
82 changes: 82 additions & 0 deletions C++/linear_search_using_recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Question: Linear Search using Recursion

#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int key, int index)
{
if (index == n) // Base case: If the element is not found
return -1;
if (arr[index] == key) // If the element is found
return index;
return linearSearch(arr, n, key, index + 1); // Recursive call
}

int main()
{
int n, key;
cout << "Enter the number of elements in the array: ";
cin >> n; // Input the number of elements in the array
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++)
cin >> arr[i]; // Input the elements of the array
cout << "Enter the element to search: ";
cin >> key; // Input the element to search
int index = linearSearch(arr, n, key, 0); // Function call
if (index != -1)
cout << "Element found at index: " << index << endl; // Output if element is found
else
cout << "Element not found" << endl; // Output if element is not found
return 0;
}

/*
**Explanation:**

1. Function Declaration (linearSearch)

The function linearSearch is declared with four parameters:
arr[]: the array containing the elements.
n: the number of elements in the array or the current size of the array for each recursive call.
key: the element to search in the array.
index: the current index being checked in the array.

2. Base Case (if (index == n))

The base case checks if the current index is equal to the size of the array (n). If the index reaches n, it means the element is not found in the array, and the function returns -1.

3. Recursive Call (linearSearch(arr, n, key, index + 1))

If the element is not found at the current index, the function makes a recursive call with the next index (index + 1) to search for the element in the remaining array.

4. Main Function

Input:
The program first asks the user for the number of elements (n) in the array.
Then, the user is prompted to enter the elements of the array one by one.
The user is also asked to enter the element (key) to search in the array.

Output:
The linearSearch function is called with the entire array, its size (n), the element to search (key), and the starting index (0).
If the element is found, the function returns the index where the element is found, and the program outputs "Element found at index: <index>".
If the element is not found, the function returns -1, and the program outputs "Element not found".

## Example Walkthrough:
If the input is:

n = 5
arr = [10, 20, 30, 40, 50]
key = 30

The recursive process looks like this:

linearSearch(arr, 5, 30, 0) = linearSearch(arr, 5, 30, 1)
linearSearch(arr, 5, 30, 1) = linearSearch(arr, 5, 30, 2)
linearSearch(arr, 5, 30, 2) = 2 (element found at index 2)

Output:
Element found at index: 2


*/
73 changes: 73 additions & 0 deletions C++/sum_of_array_using_recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Question: Find the sum of the elements of an array using recursion.

#include <iostream>
using namespace std;

int sum(int arr[], int n)
{
if (n <= 0) // Base case
return 0;
return sum(arr, n - 1) + arr[n - 1]; // Recursive call
}

int main()
{
int n;
cout << "Enter the number of elements in the array: ";
cin >> n; // Input the number of elements in the array
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++)
cin >> arr[i]; // Input the elements of the array
cout << "Sum of the elements of the array: " << sum(arr, n); // Output the sum of the elements of the array
return 0;
}

/*
**Explanation:**

1. Function Declaration (sum)

The function sum is declared with two parameters:
arr[]: the array containing the elements.
n: the number of elements in the array or the current size of the array for each recursive call.

2. Base Case (if (n <= 0))

Recursion needs a stopping condition to avoid infinite calls. The base case here is when n <= 0, meaning there are no more elements to sum up.
If this condition is true, the function returns 0 because there are no elements left to sum. This terminates the recursion.

3. Recursive Call (sum(arr, n - 1) + arr[n - 1])

The key idea in recursion is to break the problem into smaller parts. Here, we make a recursive call by reducing the size of the array by 1 (n - 1).
Each recursive call sums up the element arr[n - 1] (the last element in the current array) and adds it to the sum of the remaining array (sum(arr, n - 1)).
This continues until n becomes 0, at which point the recursion stops, and the base case returns 0.

4. Main Function

Input:
The program first asks the user for the number of elements (n) in the array.
Then, the user is prompted to enter the elements of the array one by one.
Output:
The sum function is called with the entire array and its size (n), and the result (sum of the array elements) is printed.


## Example Walkthrough:
If the input is:

n = 4
arr = [1, 2, 3, 4]

The recursive process looks like this:

sum(arr, 4) = sum(arr, 3) + arr[3] = sum(arr, 3) + 4
sum(arr, 3) = sum(arr, 2) + arr[2] = sum(arr, 2) + 3
sum(arr, 2) = sum(arr, 1) + arr[1] = sum(arr, 1) + 2
sum(arr, 1) = sum(arr, 0) + arr[0] = sum(arr, 0) + 1
sum(arr, 0) = 0 (base case)

Finally, summing up all the values:
0 + 1 + 2 + 3 + 4 = 10


*/