-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec34_rec4.cpp
182 lines (143 loc) · 2.49 KB
/
lec34_rec4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Reverse of a string using recursion
/*
#include <iostream>
using namespace std;
void reverse(string &s, int i, int j)
{
// base case
if (i > j)
return;
swap(s[i], s[j]);
i++;
j--;
reverse(s, i, j);
}
int main()
{
string a = "abcde";
reverse(a, 0, a.length() - 1);
cout << "Reverse of string -> " << a << endl;
string name = "xy";
reverse(name, 0, name.length() - 1);
cout << "Reverse of string -> " << name << endl;
}
*/
// check palindrome -1
/*
#include <iostream>
using namespace std;
void reverse(string &s, int i, int j)
{
// base case
if (i > j)
return;
swap(s[i], s[j]);
i++;
j--;
reverse(s, i, j);
}
bool checkpal(string str)
{
string s = str;
reverse(str,0,str.length()-1);
if(s==str)
return true;
else
return false;
}
int main()
{
string a = "xyz";
string b = "abbabba";
cout<<checkpal(a)<<endl;
cout<<checkpal(b);
}
*/
//Check plaindrome -2
/*
#include <iostream>
using namespace std;
bool checkpal(string str, int i, int j)
{
// base case
if (i > j)
return true;
if (str[i] != str[j])
return false;
else
{
return checkpal(str, i + 1, j - 1);
}
}
int main()
{
string a = "xyz";
string b = "abbabba";
if (checkpal(a, 0, a.length() - 1))
cout << a << " is a palindrome" << endl;
else
cout << a << " is not a palindrome" << endl;
if (checkpal(b, 0, b.length() - 1))
cout << b << " is a palindrome" << endl;
else
cout << b << " is not a palindrome" << endl;
}
*/
//a^b
/*
#include<iostream>
using namespace std;
int power(int a,int b)
{
//base case
if(b==0)
return 1;
if(b==1)
return a;
int ans= power(a,b/2);
if(b%2==0)
{
return ans*ans;
}
else{
return a*ans*ans;
}
}
int main()
{
int a,b;
cin>>a>>b;
cout<<a<<"^"<<b<<" == "<<power(a,b);
}*/
//BUbble sort
/*
#include<iostream>
using namespace std;
void printarr(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void bubblesort(int arr[],int size)
{
if(size==0 || size==1)
return ;
for(int i=0;i<size;i++){
if(arr[i]>arr[i+1])
swap(arr[i],arr[i+1]);
}
//Recursive call
bubblesort(arr,size-1);
}
int main()
{
int arr[5] = {6, 2, 4, 8, 10};
bubblesort(arr, 5);
printarr(arr,5);
int a[5] = {7, 1, 4, 3, 9};
bubblesort(a, 5);
}
*/