Repository navigation
Expand file tree
/
Copy pathLecture34.cpp
More file actions
168 lines (126 loc) · 3.15 KB
/
Copy pathLecture34.cpp
File metadata and controls
168 lines (126 loc) · 3.15 KB
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
#include<iostream>
using namespace std;
// insertion sort //
void insertionSort(int n, vector<int> &arr, int i){
if(n==0 || n==1)
{
return;
}
int temp = arr[i];
int j = i-1;
for(; j>=0; j--) {
if(arr[j] > temp) {
//shift
arr[j+1] = arr[j];
}
else { // ruk jao
break;
}
}
//copy temp value
arr[j+1] = temp;
insertionSort(n,arr,i+1)
}
// selection sort //
void selectionSort(int* arr, int n)
{
if(n==0 || n==1)
{
return;
}
int min = INT_MAX;
int inx = 0;
for(int i = 0;i<n;i++)
{
if(arr[i]<min)
{
min = arr[i];
inx = i;
}
}
swap(arr[0],arr[inx]);
selectionSort(arr+1,n-1);
}
// bubble sort //
void sortArray(int *arr, int n) {
//base case - already sorted
if(n == 0 || n == 1) {
return ;
}
//1 case sovle karlia - largest element ko end me rakh dega
for(int i=0; i<n-1; i++) {
if(arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
}
}
//Recursive Call
sortArray(arr, n-1);
}
int power(int a, int b) {
//base case
if( b == 0 )
return 1;
if(b == 1)
return a;
//RECURSIVE CALL
int ans = power(a, b/2);
//if b is even
if(b%2 == 0) {
return ans * ans;
}
else {
//if b is odd
return a * ans * ans;
}
}
void reverse(string& str, int i) {
cout << "call recieved for " << str << endl;
//base case
int n = str.length();
if(i>(n-i-1))
return ;
//processing
swap(str[i], str[n-i-1]);
i++;
//Recursive call
reverse(str,i);
}
bool checkPalindrome(string str, int i, int j) {
//base case
if(i>j)
return true;
if(str[i] != str[j])
return false;
else{
//Recursive call
return checkPalindrome(str, i+1,j-1);
}
}
int main() {
string name1 = "abcde";
cout << endl;
reverse(name1, 0);
cout << endl;
cout << name1 << endl;
string name2 = "BookkooB";
cout << endl;
bool isPalindrome = checkPalindrome(name2, 0, name2.length()-1 );
if(isPalindrome) {
cout << "Its a Palindrome " << endl;
}
else {
cout << "Its not a Palindrome " << endl;
}
int a,b;
cin >> a >> b;
cout << endl;
int ans = power(a,b);
cout << "Answer is " << ans << endl;
int arr[5] = {2,5,1,6,9};
sortArray(arr,5);
for(int i=0; i<5; i++) {
cout << arr[i] << " ";
}cout << endl;
return 0;
}
// selection sort and insertion sort pending