0% found this document useful (0 votes)
11 views

SWAYANSHU ROUT - Divide and Conquer

The document contains various implementations of algorithms in C++, including factorial calculations (both iterative and recursive), Fibonacci sequences (both recursive and iterative), string reversal (recursive and iterative), Catalan numbers, Tower of Hanoi, sorting algorithms (Merge Sort and Bubble Sort), and search algorithms (Linear and Binary Search). Each algorithm is accompanied by a main function demonstrating its usage. The code snippets illustrate fundamental programming concepts and techniques in C++.

Uploaded by

SWAYANSHU ROUT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SWAYANSHU ROUT - Divide and Conquer

The document contains various implementations of algorithms in C++, including factorial calculations (both iterative and recursive), Fibonacci sequences (both recursive and iterative), string reversal (recursive and iterative), Catalan numbers, Tower of Hanoi, sorting algorithms (Merge Sort and Bubble Sort), and search algorithms (Linear and Binary Search). Each algorithm is accompanied by a main function demonstrating its usage. The code snippets illustrate fundamental programming concepts and techniques in C++.

Uploaded by

SWAYANSHU ROUT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Factorial using iterative function

Factorial using recursion


Fibonacci using recursion
Fibonacci using iteration
Recursive String Reversal
#include <iostream>
#include <string>

std::string recursive_reverse(const std::string& s) {


if (s.length() <= 1) {
return s;
} else {
return recursive_reverse(s.substr(1)) + s[0];
}
}

int main() {
std::string s = "Hello, World!";
std::string reversed_s = recursive_reverse(s);
std::cout << "Original string: " << s << std::endl;
std::cout << "Reversed string: " << reversed_s << std::endl;
return 0;
}

Iterative String Reversal


#include <iostream>
#include <string>

std::string iterative_reverse(const std::string& s) {


std::string reversed_string = "";
for (char c : s) {
reversed_string = c + reversed_string;
}
return reversed_string;
}

int main() {
std::string s = "Hello, World!";
std::string reversed_s = iterative_reverse(s);
std::cout << "Original string: " << s << std::endl;
std::cout << "Reversed string: " << reversed_s << std::endl;
return 0;
}

Recursive Function for Catalan Number


#include <iostream>
int catalan(int n) {
if (n <= 1) {
return 1;
}

int result = 0;
for (int i = 0; i < n; i++) {
result += catalan(i) * catalan(n - i - 1);
}
return result;
}

int main() {
int n = 5;
std::cout << "Catalan number C(" << n << ") = " << catalan(n) <<
std::endl;
return 0;
}

Tower of Hanoi
#include <iostream>

void towerOfHanoi(int n, char source, char destination, char auxiliary)


{
if (n == 1) {
std::cout << "Move disk 1 from " << source << " to " <<
destination << std::endl;
return;
}
towerOfHanoi(n - 1, source, auxiliary, destination);

std::cout << "Move disk " << n << " from " << source << " to " <<
destination << std::endl;

towerOfHanoi(n - 1, auxiliary, destination, source);


}

int main() {
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); return 0;
}

Merge sort

#include <iostream>
#include <vector>

void merge(std::vector<int>& arr, int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;

std::vector<int> leftArr(n1), rightArr(n2);

for (int i = 0; i < n1; i++)


leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = leftArr[i];
i++;
k++;
}

while (j < n2) {


arr[k] = rightArr[j];
j++;
k++;
}
}

void mergeSort(std::vector<int>& arr, int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

int main() {
std::vector<int> arr = {12, 11, 13, 5, 6, 7};
int arr_size = arr.size();

mergeSort(arr, 0, arr_size - 1);

std::cout << "Sorted array: \n";


for (int i = 0; i < arr_size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;

return 0;
}

Bubble sort

#include <iostream>
#include <vector>

void bubbleSort(std::vector<int>& arr) {


int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
}
}

int main() {
std::vector<int> arr = {64, 34, 25, 12, 22, 11, 90};

bubbleSort(arr);

std::cout << "Sorted array: \n";


for (int i = 0; i < arr.size(); i++)
std::cout << arr[i] << " ";
std::cout << std::endl;

return 0;
}

Linear Search

#include <iostream>
#include <vector>

int linearSearch(const std::vector<int>& arr, int target) {


for (int i = 0; i < arr.size(); i++) {
if (arr[i] == target) {
return i; // Target found, return its index
}
}
return -1; // Target not found
}

int main() {
std::vector<int> arr = {10, 23, 19, 2, 15, 7, 8};
int target = 15;

int index = linearSearch(arr, target);

if (index != -1) {
std::cout << "Element found at index: " << index << std::endl;
} else {
std::cout << "Element not found in the array." << std::endl;
}

return 0;
}

Binary search
#include <iostream>
#include <vector>

int binarySearch(const std::vector<int>& arr, int target) {


int left = 0;
int right = arr.size() - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid; // Target found, return its index
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // Target not found
}

int main() {
std::vector<int> arr = {2, 7, 8, 10, 15, 19, 23}; // Sorted array
int target = 15;

int index = binarySearch(arr, target);

if (index != -1) {
std::cout << "Element found at index: " << index << std::endl;
} else {
std::cout << "Element not found in the array." << std::endl;
}

return 0;
}

You might also like