Day 6 of 100 Days of DSA Challenge: Exploring Recursion

Day 6 of 100 Days of DSA Challenge: Exploring Recursion

Welcome back to another update on my 100 Days of Data Structures and Algorithms challenge. Today was an engaging day where I delved into the realm of recursion, implementing it to solve some fundamental problems. Recursion, a powerful concept in computer science, involves solving problems by breaking them down into smaller, more manageable subproblems. Let's dive into the code and explore what I accomplished on this exciting journey.

1. String Reverse

The task was to reverse a string using recursion. Here's the code snippet I worked on:

#include <bits/stdc++.h>
using namespace std;

void reverseString(string& str, int i, int j) {
    if (i > j) {
        return;
    }
    swap(str[i], str[j]);
    reverseString(str, i + 1, j - 1);
}

int main() {
    string str = "abcdef";
    int i = 0, j = str.size() - 1;
    reverseString(str, i, j);
    cout << str << endl;
}

In this code, I used a recursive approach to reverse the string by swapping characters from both ends, gradually moving towards the center until the entire string is reversed.

2. Check Palindrome

Next, I implemented a function to check whether a given string is a palindrome or not. Here’s the code:

#include <bits/stdc++.h>
using namespace std;

bool isPalindrome(string str, int i, int j) {
    if (i > j) {
        return true;
    }
    if (str[i] != str[j]) {
        return false;
    } else {
        return isPalindrome(str, i + 1, j - 1);
    }
}

int main() {
    string str = "abbac";
    int i = 0, j = str.size() - 1;
    bool palindrome = isPalindrome(str, i, j);
    if (palindrome) {
        cout << "String is a Palindrome" << endl;
    } else {
        cout << "String is not a Palindrome" << endl;
    }
}

Here, I utilized recursion to check whether the given string is a palindrome or not by comparing characters from both ends towards the center.

3. Power Calculation

I also delved into calculating the power of a number using recursion. The code snippet is as follows:

#include <bits/stdc++.h>
using namespace std;

int power(int a, int b) {
    if (b == 0)
        return 1;
    if (b == 1)
        return a;
    int result = power(a, b / 2);
    if (b % 2 == 0) {
        return result * result;
    } else {
        return a * result * result;
    }
}

int main() {
    int base = 2, exponent = 10;
    int answer = power(base, exponent);
    cout << answer << endl;
}

This function uses recursion to efficiently compute the power of a given number by considering both even and odd exponents.

4. Bubble Sort

Lastly, I implemented the bubble sort algorithm using recursion:

#include <bits/stdc++.h>
using namespace std;

void bubbleSort(int *arr, int n) {
    if (n == 0 || n == 1) {
        return;
    }
    for (int i = 0; i < n - 1; i++) {
        if (arr[i] > arr[i + 1]) {
            swap(arr[i], arr[i + 1]);
        }
    }
    bubbleSort(arr, n - 1);
}

int main() {
    int arr[5] = {5, 4, 3, 2, 1};
    bubbleSort(arr, 5);
    for (auto it : arr) {
        cout << it << " ";
    }
}

This implementation of the bubble sort algorithm recursively sorts an array.

Today's journey into recursion was exhilarating. These code snippets allowed me to grasp the essence and power of recursion in problem-solving. Stay tuned for more updates in the upcoming days as I continue my DSA challenge!

Happy Coding! ✨🖥️


Feel free to enhance, modify, or add more personal anecdotes or explanations to the draft to better suit your style and experience during the challenge.