SWAYANSHU ROUT - Divide and Conquer
SWAYANSHU ROUT - Divide and Conquer
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;
}
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;
}
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>
std::cout << "Move disk " << n << " from " << source << " to " <<
destination << std::endl;
int main() {
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); return 0;
}
Merge sort
#include <iostream>
#include <vector>
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++;
}
int main() {
std::vector<int> arr = {12, 11, 13, 5, 6, 7};
int arr_size = arr.size();
return 0;
}
Bubble sort
#include <iostream>
#include <vector>
int main() {
std::vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
return 0;
}
Linear Search
#include <iostream>
#include <vector>
int main() {
std::vector<int> arr = {10, 23, 19, 2, 15, 7, 8};
int target = 15;
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>
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;
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;
}