Skip to content

Commit b9058ae

Browse files
Mohit JainMohit Jain
Mohit Jain
authored and
Mohit Jain
committed
recursion
1 parent 428ef55 commit b9058ae

13 files changed

+516
-5
lines changed

2048Recursion.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// Problem
6+
// Number to spellings
7+
// 2048 -----> two zero four eight
8+
9+
char words[][10] = {"zero","one", "two", "three", "four",
10+
"five", "six", "seven", "eight", "nine"};
11+
12+
void printSpellings(int n){
13+
if(n ==0) return;
14+
15+
printSpellings(n/10);
16+
int digit = n%10;
17+
18+
cout<< words[digit]<<" ";
19+
20+
return;
21+
22+
}
23+
24+
int main() {
25+
26+
int n;
27+
28+
cin>>n;
29+
30+
printSpellings(n);
31+
32+
return 0;
33+
}

BubbleSortRecursion.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <bits/stdc+.h>
2+
3+
using namespace std;
4+
5+
6+
// Bubble Sort Recursively
7+
8+
void bubble_sort(int a[], int n){
9+
10+
if(n == 1) return;
11+
12+
for(int j =0; j <n-1; j ++) {
13+
if(a[j]>a[j+1]){
14+
swap(a[j],a[j+1]);
15+
}
16+
}
17+
18+
bubble_sort(a,n-1);
19+
20+
21+
}
22+
23+
void bubble_sort_recursive(int a[], int j, int n) {
24+
// base case
25+
if(n == 1) return;
26+
27+
if(j == n-1) {
28+
return bubble_sort_recursive(a, 0, n-1);
29+
}
30+
if(a[j]>a[j+1]) {
31+
swap(a[j],a[j+1]);
32+
}
33+
34+
bubble_sort_recursive(a,j+1,n);
35+
36+
return;
37+
}
38+
39+
int main() {
40+
41+
int a[] = {5,4,3,2,1};
42+
int n= 5;
43+
44+
return 0;
45+
}

Fast_power.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
6+
7+
int power(int a, int n) {
8+
9+
if(n == 0 ) return 1;
10+
11+
return a*power(a,n-1);
12+
}
13+
14+
int fast_power(int a, int n) {
15+
16+
if (n == 0) return 1;
17+
18+
19+
int subAns = fast_power(a, n/2);
20+
21+
subAns *= subAns;
22+
23+
if(n&1) return subAns*a;
24+
25+
return subAns;
26+
27+
}
28+
29+
int main() {
30+
31+
int a,n;
32+
33+
cin>>a>>n;
34+
35+
fast_power(a,n);
36+
37+
return 0;
38+
}

input.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
1
2-
132
1+
1234

inversionCount.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
6+
int merge(int *a, int s, int e) {
7+
8+
int mid = (s+e) / 2;
9+
10+
int i =s;
11+
int j = (mid+1);
12+
13+
int k =s;
14+
15+
int temp[100000];
16+
17+
int cnt = 0;
18+
19+
while(i <=mid and j<=e) {
20+
21+
if(a[i]<= a[j]){
22+
temp[k++] = a[i++];
23+
}
24+
25+
else {
26+
temp[k++] = a[j++];
27+
cnt+= mid-i+1;
28+
}
29+
30+
31+
}
32+
while(i<=mid){
33+
temp[k++] = a[i++];
34+
}
35+
36+
while(j<=e){
37+
temp[k++] = a[j++];
38+
}
39+
40+
for(int i = s;i<=e; i++) {
41+
a[i] = temp[i];
42+
}
43+
44+
return cnt;
45+
}
46+
47+
48+
int inversion_count(int *arr, int s, int e) {
49+
50+
if(s>=e){
51+
return 0;
52+
}
53+
54+
int mid = (s+e)/2;
55+
56+
int x = inversion_count(a,s,mid);
57+
int y = inversion_count(a,mid+1,e);
58+
59+
int z = merge(a,s,e);
60+
61+
return x+y+z;
62+
63+
64+
65+
}
66+
67+
int main() {
68+
69+
int arr[] = {1,5,2,6,3,0};
70+
71+
int n = sizeof(arr)/ sizeof(int);
72+
73+
74+
return 0;
75+
}

mergeSortRecursive.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
6+
void merge(int *a, int s, int e) {
7+
8+
int mid = (s+e)/2;
9+
10+
int i= s;
11+
12+
int j = mid+1;
13+
14+
int k = s;
15+
int temp[100];
16+
17+
while(i<=mid && j<=e) {
18+
if(a[i] <a[j]) {
19+
temp[k++] = a[i++];
20+
21+
}
22+
else {
23+
temp[k++] = a[j++];
24+
}
25+
}
26+
27+
while(i <= mid) {
28+
temp[k++] = a[i++];
29+
}
30+
31+
while(j <=e) {
32+
temp[k++] = a[j++];
33+
}
34+
35+
for(int i = s;i<=e; i++) {
36+
a[i] = temp[i];
37+
}
38+
}
39+
40+
void mergeSort(int a[], int s , int e) {
41+
42+
43+
//Base Case
44+
if (s >=e) {
45+
return;
46+
}
47+
48+
int mid = (s+e) /2;
49+
50+
mergeSort(a,s,mid);
51+
52+
mergeSort(a,mid+1,e);
53+
54+
merge(a,s,e);
55+
56+
57+
58+
59+
60+
61+
62+
63+
}
64+
65+
66+
int main() {
67+
68+
int a[100];
69+
70+
int n;
71+
72+
cin>>n;
73+
74+
for(int i=0; i<n; i++) {
75+
cin>>a[i];
76+
}
77+
mergeSort(a,0,n-1);
78+
79+
80+
return 0;
81+
}

output.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
2
2-
3
3-
11
1+
1234

quickSort

33.1 KB
Binary file not shown.

quickSort.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int partitiona(int *a, int s, int e) {
6+
7+
8+
int i = s - 1;
9+
int j = s;
10+
int pivot = a[e];
11+
for(;j <=e-1;){
12+
if(a[j]<=pivot) {
13+
14+
i = i+1;
15+
swap(a[i],a[j]);
16+
}
17+
j= j+1;
18+
}
19+
20+
swap(a[i+1], a[e]);
21+
22+
return i +1;
23+
}
24+
25+
void quicksort(int *arr, int s, int e) {
26+
27+
28+
if(s>=e) return;
29+
30+
int p = partitiona(arr,s,e);
31+
32+
quicksort(arr,s,p-1);
33+
34+
quicksort(arr,p+1, e);
35+
}
36+
37+
38+
int main() {
39+
40+
41+
int arr[] = {2,7,8,6,1,5,4};
42+
43+
int n = sizeof(arr) / sizeof(int);
44+
45+
quicksort(arr,0,n-1);
46+
47+
48+
49+
return 0;
50+
51+
}

0 commit comments

Comments
 (0)