forked from SAMYAK99/CS-180
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchingOOPS.cpp
448 lines (371 loc) · 7.78 KB
/
SearchingOOPS.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
class Array
{
private:
int *A;
int size;
int length;
void swap(int *x, int *y);
public:
Array()
{
size = 10;
length = 0;
A = new int[size];
}
Array(int sz)
{
size = sz;
length = 0;
A = new int[size];
}
~Array()
{
delete []A;
}
void Display();
void Append(int x);
void Insert(int index, int x);
int Delete(int index);
int LinearSearch(int key);
int BinarySearch(int key);
//int RBinSearch(int a[],int l,int h,int key);
int Get(int index);
void Set(int index, int x);
int Max();
int Min();
int Sum();
float Avg();
void Reverse();
void Reverse2();
void InsertSort(int x);
int isSorted();
void Rearrange();
Array* Merge(Array *arr2);
Array* Union(Array *arr2);
Array* Intersection(Array *arr2);
Array* Difference(Array *arr2);
};
void Array::Display()
{
int i;
printf("\nElements are:\n");
for(i=0;i<length;i++)
printf("%d ",A[i]);
}
void Array::Append(int x)
{
if(length < size)
A[length++] = x;
}
void Array::Insert(int index, int x)
{
int i;
if(index>=0 && index<=length)
{
for(i=length;i>index;i--)
A[i] = A[i-1];
A[index] = x;
length++;
}
}
int Array::Delete(int index)
{
int x=0;
int i;
if(index>=0 && index<length)
{
x = A[index];
for(i=index;i<length-1;i++)
A[i] = A[i+1];
length--;
return x;
}
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int Array::LinearSearch(int key)
{
int i;
for(i=0;i<length;i++)
{
if(key == A[i])
{
swap(&A[i],&A[0]);
return i;
}
}
return -1;
}
int Array::BinarySearch(int key)
{
int l,mid,h;
l=0;
h=length-1;
while(l<=h)
{
mid = (l+h)/2;
if(key == A[mid])
return mid;
else if(key < A[mid])
h=mid-1;
else
l = mid+1;
}
return -1;
}
int RBinSearch(int a[],int l,int h,int key)
{
int mid;
if(l<=h)
{
mid = (l+h)/2;
if(key == a[mid])
return mid;
else if(key<a[mid])
return RBinSearch(a,l,mid-1,key);
else
return RBinSearch(a,mid+1,h,key);
}
}
int Array::Get(int index)
{
if(index>=0 && index<length)
return A[index];
return -1;
}
void Array::Set(int index, int x)
{
if(index>=0 && index<length)
A[index] = x;
}
int Array::Max()
{
int max = A[0];
int i;
for(i=0;i<length;i++)
{
if(A[i] > max)
max = A[i];
}
return max;
}
int Array::Min()
{
int min = A[0];
int i;
for(i=0;i<length;i++)
{
if(A[i] < min)
min = A[i];
}
return min;
}
int Array::Sum()
{
int s=0;
int i;
for(i=0;i<length;i++)
s += A[i];
return s;
}
float Array::Avg()
{
return (float)(Sum(arr)/length);
}
void Reverse(struct Array *arr)
{
int *B;
int i,j;
B = (int *)malloc(arr->length*sizeof(int));
for(i=arr->length-1,j=0;i>=0;i--,j++)
{
B[j] = arr->A[i];
}
for(i=0;i<arr->length;i++)
arr->A[i] = B[i];
}
void Reverse2(struct Array *arr)
{
int i,j;
for(i=0,j=arr->length-1;i<j;i++,j--)
{
swap(&arr->A[i],&arr->A[j]);
}
}
void InsertSort(struct Array *arr, int x)
{
int i=arr->length-1;
if(arr->length == arr->size)
return;
while(i>=0 && arr->A[i] > x)
{
arr->A[i+1] = arr->A[i];
i--;
}
arr->A[i+1] = x;
arr->length++;
}
int isSorted(struct Array arr)
{
int i;
for(i=0;i<arr.length-1;i++)
{
if(arr.A[i]>arr.A[i+1])
return 0;
}
return 1;
}
void Rearrange(struct Array *arr)
{
int i,j;
i=0;
j = arr->length-1;
while(i<j)
{
while(arr->A[i]<0)
i++;
while(arr->A[j]>=0)
j--;
if(i<j)
swap(&arr->A[i],&arr->A[j]);
}
}
struct Array* Merge(struct Array *arr1, struct Array *arr2)
{
int i,j,k;
i=j=k=0;
struct Array *arr3 = (struct Array *)malloc(sizeof(struct Array));
while(i<arr1->length && j<arr2->length)
{
if(arr1->A[i]<arr2->A[j])
arr3->A[k++] = arr1->A[i++];
else
arr3->A[k++] = arr2->A[j++];
}
for(;i<arr1->length;i++)
arr3->A[k++] = arr1->A[i];
for(;j<arr2->length;j++)
arr3->A[k++] = arr2->A[j];
arr3->length = arr1->length + arr2->length;
arr3->size = 10;
return arr3;
}
struct Array* Union(struct Array *arr1, struct Array *arr2)
{
int i,j,k;
i=j=k=0;
struct Array *arr3 = (struct Array *)malloc(sizeof(struct Array));
while(i<arr1->length && j<arr2->length)
{
if(arr1->A[i]<arr2->A[j]) //unequal hai to dono ko add kro C array me
arr3->A[k++] = arr1->A[i++];
else if(arr2->A[j]<arr1->A[i])
arr3->A[k++] = arr2->A[j++];
else
{
arr3->A[k++] = arr1->A[i++]; //equal hai to kissi ek ko add kro C array me
j++;
}
}
for(;i<arr1->length;i++)
arr3->A[k++] = arr1->A[i];
for(;j<arr2->length;j++)
arr3->A[k++] = arr2->A[j];
arr3->length = k;
arr3->size = 10;
return arr3;
}
struct Array* Intersection(struct Array *arr1, struct Array *arr2)
{
int i,j,k;
i=j=k=0;
struct Array *arr3 = (struct Array *)malloc(sizeof(struct Array));
while(i<arr1->length && j<arr2->length)
{
if(arr1->A[i]<arr2->A[j])
i++;
else if(arr2->A[j]<arr1->A[i])
j++;
else if(arr1->A[i] == arr2->A[j]) //if both are equal copy any one of them into C array
{
arr3->A[k++] = arr1->A[i++];
j++;
}
}
arr3->length = k;
arr3->size = 10;
return arr3;
}
struct Array* Difference(struct Array *arr1, struct Array *arr2)
{
int i,j,k;
i=j=k=0;
struct Array *arr3 = (struct Array *)malloc(sizeof(struct Array));
while(i<arr1->length && j<arr2->length)
{
if(arr1->A[i]<arr2->A[j]) //unequal hai to use add kro C array me A se A-B me
arr3->A[k++] = arr1->A[i++];
else if(arr2->A[j]<arr1->A[i])
j++;
else //dono equal hai to ++ karke ignore kro
{
i++;
j++;
}
}
for(;i<arr1->length;i++)
arr3->A[k++] = arr1->A[i];
arr3->length = k;
arr3->size = 10;
return arr3;
}
int main()
{
struct Array arr1;
int ch,x,index;
printf("Enter size of Array");
scanf("%d",&arr1.size);
arr1.A = (int *)malloc(arr1.size*sizeof(int));
do
{
printf("Menu\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Sum\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter an element and index");
scanf("%d%d",&x,&index);
Insert(&arr1,index,x);
break;
case 2: printf("Enter index");
scanf("%d",&index);
x = Delete(&arr1,index);
printf("Deleted element is %d\n",x);
break;
case 3: printf("Enter element to search");
scanf("%d",x);
index = LinearSearch(&arr1,x);
printf("Element index %d",index);
break;
case 4: printf("Sum is %d\n",Sum(arr1));
break;
case 5: Display(arr1);
}
} while(ch<6);
return 0;
}