Skip to content

Commit 5e92fca

Browse files
committed
Completed Recursion - 2
1 parent d0a1620 commit 5e92fca

File tree

6 files changed

+473
-0
lines changed

6 files changed

+473
-0
lines changed

F19Recursion-2/MergeSort.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Title: Merge Sort
3+
4+
Problem statement
5+
You are given the starting 'l' and the ending 'r' positions of the array 'ARR'.
6+
You must sort the elements between 'l' and 'r'.
7+
8+
Note:
9+
Change in the input array itself. So no need to return or print anything.
10+
Example:
11+
Input: ‘N’ = 7,
12+
'ARR' = [2, 13, 4, 1, 3, 6, 28]
13+
14+
Output: [1 2 3 4 6 13 28]
15+
16+
Detailed explanation ( Input/output format, Notes, Images )
17+
Input format :
18+
The first line contains an integer <em>**'N'**</em> representing the size of the array/list.
19+
The second line contains 'N' single space-separated integers representing the elements in
20+
the array/list.
21+
22+
Output format :
23+
You don't need to return anything. In the output, you will see the array after you do the
24+
modification.
25+
26+
Sample Input 1:
27+
7
28+
2 13 4 1 3 6 28
29+
30+
Sample Output 1:
31+
1 2 3 4 6 13 28
32+
33+
Explanation of Sample Output 1:
34+
After applying 'merge sort' on the input array, the output is [1 2 3 4 6 13 28].
35+
36+
Sample Input 2:
37+
5
38+
9 3 6 2 0
39+
40+
Sample Output 2:
41+
0 2 3 6 9
42+
43+
Explanation of Sample Output 2:
44+
After applying 'merge sort' on the input array, the output is [0 2 3 6 9].
45+
46+
Constraints :
47+
1 <= N <= 10^3
48+
0 <= ARR[i] <= 10^9
49+
*/
50+
51+
#include <iostream>
52+
using namespace std;
53+
54+
void Merge(int array[], int l, int mid, int r) {
55+
int left_size = (mid - l) + 1;
56+
int right_size = (r - mid);
57+
58+
int array_left[left_size];
59+
int array_right[right_size];
60+
61+
for (int i = 0; i < left_size; i++) {
62+
array_left[i] = array[l + i];
63+
}
64+
65+
for (int i = 0; i < right_size; i++) {
66+
array_right[i] = array[mid + 1 + i];
67+
}
68+
69+
int idx_left = 0;
70+
int idx_right = 0;
71+
int idx_array = l;
72+
73+
while (idx_left < left_size && idx_right < right_size) {
74+
if (array_left[idx_left] <= array_right[idx_right]) {
75+
array[idx_array++] = array_left[idx_left++];
76+
} else {
77+
array[idx_array++] = array_right[idx_right++];
78+
}
79+
}
80+
81+
while (idx_left < left_size) {
82+
array[idx_array++] = array_left[idx_left++];
83+
}
84+
85+
while (idx_right < right_size) {
86+
array[idx_array++] = array_right[idx_right++];
87+
}
88+
}
89+
90+
void MergeSort(int array[], int l, int r) {
91+
if (r - l < 1) {
92+
return;
93+
}
94+
95+
int mid = (l + r) / 2;
96+
MergeSort(array, l, mid);
97+
MergeSort(array, mid + 1, r);
98+
Merge(array, l, mid, r);
99+
}
100+
101+
int main() {
102+
int n;
103+
cin >> n;
104+
105+
int arr[n];
106+
for (int i = 0; i < n; i++) {
107+
cin >> arr[i];
108+
}
109+
110+
int arr_size = sizeof(arr) / sizeof(arr[0]);
111+
112+
MergeSort(arr, 0, arr_size - 1);
113+
114+
for (int i = 0; i < arr_size; i++) {
115+
cout << arr[i] << " ";
116+
}
117+
cout << endl;
118+
return 0;
119+
}

F19Recursion-2/QuickSort.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Title: Quick Sort
3+
4+
Problem statement
5+
Given the 'start' and the 'end'' positions of the array 'input'. Your task is to sort the elements
6+
between 'start' and 'end' using quick sort.
7+
8+
Note : Make changes in the input array itself.
9+
10+
Detailed explanation ( Input/output format, Notes, Images )
11+
Input format :
12+
Line 1 : Integer N i.e. Array size
13+
Line 2 : Array elements (separated by space)
14+
15+
Output format :
16+
Array elements in increasing order (separated by space)
17+
18+
Sample Input 1 :
19+
6
20+
2 6 8 5 4 3
21+
22+
Sample Output 1 :
23+
2 3 4 5 6 8
24+
25+
Sample Input 2 :
26+
5
27+
1 2 3 5 7
28+
29+
Sample Output 2 :
30+
1 2 3 5 7
31+
32+
Constraints :
33+
1 <= N <= 10^3
34+
0 <= input[i] <= 10^9
35+
*/
36+
37+
#include<iostream>
38+
#include<vector>
39+
using namespace std;
40+
41+
int Partition(int arr[], int low, int high) {
42+
int idxPivot = high;
43+
int idxSmallerElement = low-1;
44+
45+
for(int i = low; i <= high; i++) {
46+
if(arr[i] < arr[idxPivot]) {
47+
idxSmallerElement++;
48+
49+
int temp = arr[i];
50+
arr[i] = arr[idxSmallerElement];
51+
arr[idxSmallerElement] = temp;
52+
}
53+
}
54+
55+
idxSmallerElement++;
56+
int temp = arr[idxSmallerElement];
57+
arr[idxSmallerElement] = arr[idxPivot];
58+
arr[idxPivot] = temp;
59+
60+
return idxSmallerElement;
61+
}
62+
63+
void QuickSort(int arr[], int low, int high) {
64+
if(low <= high) {
65+
int idxPartition = Partition(arr, low, high);
66+
QuickSort(arr, low, idxPartition-1);
67+
QuickSort(arr, idxPartition+1, high);
68+
}
69+
}
70+
71+
class Runner {
72+
vector<int> v;
73+
74+
public:
75+
void takeInput() {
76+
int n;
77+
cin >> n;
78+
v.resize(n);
79+
for (int i = 0; i < n; i++)
80+
cin >> v[i];
81+
}
82+
83+
void execute() {
84+
int n = (int)v.size();
85+
86+
int* input = new int[n];
87+
for (int i = 0; i < n; i++)
88+
input[i] = v[i];
89+
90+
QuickSort(input, 0, n - 1);
91+
92+
free(input);
93+
}
94+
95+
void executeAndPrintOutput() {
96+
int n = (int)v.size();
97+
98+
int* input = new int[n];
99+
for (int i = 0; i < n; i++)
100+
input[i] = v[i];
101+
102+
QuickSort(input, 0, n - 1);
103+
104+
for (int i = 0; i < n; i++)
105+
cout << input[i] << ' ';
106+
cout << '\n';
107+
108+
free(input);
109+
}
110+
};
111+
112+
int main() {
113+
Runner runner;
114+
runner.takeInput();
115+
runner.executeAndPrintOutput();
116+
return 0;
117+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Title: Remove duplicates
3+
4+
Problem statement
5+
Given a string S, remove consecutive duplicates from it recursively.
6+
7+
Detailed explanation ( Input/output format, Notes, Images )
8+
Input Format :
9+
String S
10+
11+
Output Format :
12+
Output string
13+
14+
Constraints :
15+
1 <= |S| <= 10^3
16+
where |S| represents the length of string
17+
18+
Sample Input 1 :
19+
aabccba
20+
21+
Sample Output 1 :
22+
abcba
23+
24+
Sample Input 2 :
25+
xxxyyyzwwzzz
26+
27+
Sample Output 2 :
28+
xyzwz
29+
*/
30+
31+
#include<iostream>
32+
using namespace std;
33+
34+
void RemoveConsecutiveDuplicates(char s[]) {
35+
if(s[0] == '\0') {
36+
return;
37+
}
38+
39+
RemoveConsecutiveDuplicates(&s[1]);
40+
41+
if(s[0] == s[1]) {
42+
int idx = 0;
43+
while(s[idx] != '\0') {
44+
s[idx] = s[idx+1];
45+
idx++;
46+
}
47+
}
48+
49+
return;
50+
}
51+
52+
int main() {
53+
char s[100000];
54+
cin >> s;
55+
RemoveConsecutiveDuplicates(s);
56+
cout << s << endl;
57+
}

F19Recursion-2/RemoveX.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Title: Remove X
3+
4+
Problem statement
5+
Given a string, compute recursively a new string where all 'x' chars have been removed.
6+
7+
Detailed explanation ( Input/output format, Notes, Images )
8+
Input format :
9+
String S
10+
11+
Output format :
12+
Modified String
13+
14+
Constraints :
15+
1 <= |S| <= 10^3
16+
where |S| represents the length of string S.
17+
18+
Sample Input 1 :
19+
xaxb
20+
21+
Sample Output 1:
22+
ab
23+
24+
Sample Input 2 :
25+
abc
26+
27+
Sample Output 2:
28+
abc
29+
30+
Sample Input 3 :
31+
xx
32+
33+
Sample Output 3:
34+
*/
35+
36+
#include<iostream>
37+
#include<typeinfo>
38+
using namespace std;
39+
40+
void RemoveX(char * input) {
41+
if(input[0] == '\0') {
42+
return;
43+
}
44+
45+
RemoveX(&input[1]);
46+
47+
if(input[0] == 'x') {
48+
int i = 0;
49+
while(input[i] != '\0') {
50+
input[i] = input[i+1];
51+
i++;
52+
}
53+
}
54+
55+
return;
56+
}
57+
58+
int main() {
59+
char input[100];
60+
cin.getline(input, 100);
61+
RemoveX(input);
62+
cout << input << endl;
63+
}

0 commit comments

Comments
 (0)