-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_struct.cpp
680 lines (585 loc) · 15.6 KB
/
array_struct.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#include<iostream>
#include<stdio.h>
using namespace std;
struct Array
{
int *array;
int size;
int length=0;
/*print*/
void display()
{
for ( int i = 0; i < length ; i ++)
printf("%d;",array[i]);
}
/*Methods*/
int get(int index)const
{
if ( index >= 0 && index < length)
return array[index];
return -1;
}
//Set Method
bool set(int index, int value)
{
if (index >= 0 && index < length)
{
array[index] = value;
return true;
}
return false;
}
//get Max element
int Max()
{
int max = array[0];
for ( int i = 0 ; i <length; i ++)
{
if ( array[i] > max)
max = array[i];
}
return max;
}
//get Min element
int Min()
{
int min = array[0];
for ( int i = 0 ; i <length; i ++)
{
if ( array[i] < min)
min = array[i];
}
return min;
}
//get the sum of array
int sum()
{
int s = 0;
int i;
for ( int i = 0 ; i <length ; i ++ )
s += array[i];
return s;
}
int sum_recursive( int n = -2)
{
if ( n == -2)
return sum_recursive(length);
if ( n <0 )
return 0;
else
return array[n] + sum_recursive(n-1);
}
//reverse by creating new array
void reverse_cp()//Reverse the Array by copying
{
int *new_array = new int[length];
for ( int i = length - 1, j = 0 ; i >= 0 ; i --,j ++ )
new_array[j] = array[i];
delete []array; // delete the originally pointed memory space
array = new_array;// array now points to
new_array = nullptr;
}
//reverse by swapping
void reverse_s()//Reverse the Array by swap
{
for ( int low=0, high = length-1; low < high; low ++, high --)
{
int temp = array[low];
array[low] = array[high];
array[high] = temp;
}
}
// left shift
bool left_shift(int position)
{
if( position <= length)
{
for ( int i = 0 ; i < length ; i++)
{
if( i + position >= length)
array[i] = 0;
else
array[i] = array[i + position];
}
return true;
}
return false;
}
//right shift
bool right_shift(int position)
{
if(position <= length)
{
for (int i = length -1; i >= 0 ; i -- )
{
if ( i -position < 0)
array[i] = 0;
else
array[i] = array[i-position];
}
return true;
}
return false;
}
//is sorted or not
bool isSorted() //O(n)
{
for ( int i = 1 ; i < length; i ++)
{
if(array[i-1]>array[i])
return false;
}
return true;
}
//insertion for sorted array
bool insert(int value) //O(n)
{
if(isSorted())
{
int i = 0;
for ( i = length -1; array[i]>value && i >=0 ; i--)
array[i+1] = array[i];
array[i+1] = value;
length++;
return true;
}
return false;
}
//compute average
double average()//O(n)
{
return (double)sum()/length;
}
//append new element at tail, push_back()
void append(int x) //O(1)
{
array[length] = x;
length ++;
}
bool insert(int index, int value)
{
if(index >= 0 && index < length)
{
for ( int i = length; i > index; i--)
array[i] = array[i-1];
array[index] = value;
length ++;
return true;
}
return false;
}
bool remove(int index)
{
if ( index >= 0 && index < length)
{
for ( int i = index ; i < length ; i ++ )
array[i] = array[i+1]; //shift right elements to left
length -- ;
return true;
}
return false;
}
void cluster()
{
for( int i =0, j = length - 1; i < j ;)
{
while(array[i] < 0 )
i++;
while(array[j] >0 )
j--;
if ( i < j)
swap(i,j);
}
}
int search_linear(int key)
{
for ( int i = 0; i < length; i ++ )
{
if ( key == array[i])
return i;
}
return -1;
}
int search_binary(int key)
{
int l =0, h = length-1;
while (l <= h)
{
int mid = (l+h)/2;
if ( array[mid] == key)
return mid;
else if(array[mid]>key)
h = mid - 1;
else if(array[mid]<key)
l = mid + 1;
}
return -1;
}
int search_binary_recursive(int key, int l = 0, int h = -1)
{
//switch the argument
if ( h == -1)
return search_binary_recursive(key,l,length-1);
if ( l <= h)
{
int mid = (l+h)/2;
if( key == array[mid])
return mid;
else if( key < array[mid])
return search_binary_recursive(key,l,mid-1);
else
return search_binary_recursive(key,mid+1,h);
}
return -1;
}
int search_transposition(int key)
{
for ( int i = 0 ; i < length ; i ++ )
{
if ( array[i] == key)
{
if ( i > 0)
{
swap(i,i-1);//Every time the key is searched, move it more front
return i -1;
}
else
return i;
}
}
return -1;
}
void swap(int i, int j)
{
if ( i >= 0 && j >= 0 && i < length && j < length)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
Array merge(Array array2)
{
//create a new Array pointer
Array merged_array;
merged_array.size = size + array2.size;//define the size
int *a = new int[merged_array.size];//define the embedded array
merged_array.array = a;
merged_array.length = 0;
int i = 0, j = 0, k = 0;
while ( i < length && j < array2.length)
{
if ( array[i] < array2.array[j])
{
merged_array.array[k] = array[i];
merged_array.length++;
i ++;
}
else
{
merged_array.array[k] = array2.array[j];
merged_array.length++;
j ++;
}
k++;
}
//append those are not merged
for ( ;i < length; i ++ )
merged_array.append(array[i]);
for ( ;j < array2.length; j ++ )
merged_array.append(array2.array[j]);
return merged_array;
}
Array Union_unsorted(Array& array2)
{//We cannot return a reference of local variable
//Because local variable will be destroyed as the called function terminates
Array result;
result.size = size + array2.size;
int *a = new int[size];
result.array = a;
//first copy the first array onto the new array
for(int i = 0 ; i < length; i++ )
result.append(array[i]);
for (int i = 0 ; i < array2.length;i++)
{
bool has_same = false;
for (int j = 0 ; j < result.length;j++)
{
if (array2.array[i] == array[j])
{
has_same = true;
break;
}
}
if ( has_same)
continue;
else
result.append(array2.array[i]);
}
return result;
}
Array Union_sorted(Array &array2)
{
Array result;
result.size = size + array2.size;
int *a = new int[size];
result.array = a;
int i = 0, j = 0;
while ( i < length && j < array2.length)
{
if(array[i] == array2.array[j])
{
result.append(array[i]);
i++;
j++;
}
else if ( array[i] < array2.array[j])
{
result.append(array[i]);
i ++;
}
else
{
result.append(array2.array[j]);
j++;
}
}
for ( ; i < length;i++)
result.append(array[i]);
for( ; j< array2.length; j++)
result.append(array2.array[j]);
return result;
}
Array Difference_unsorted(Array &array2)
{
//create the target Array object
Array result;
result.size = size + array2.size;
int *a = new int[size];
result.array = a;
//First Copy the first Array onto target Array
for ( int i =0; i < length ; i ++ )
{
bool has_same = false;
int j = 0;
for (; j < length ; j ++ )
{
if(array[i] == array2.array[j])
{
has_same = true;
break;
}
}
if ( !has_same)
{
result.append(array[i]);
}
}
return result;
}
Array Difference_sorted(Array &array2)
{
//create the target Array object
Array result;
result.size = size + array2.size;
int *a = new int[size];
result.array = a;
int i = 0, j = 0 ;
while ( i < length && j < array2.length)
{
if (array[i] < array2.array[j])
{
result.append(array[i]);
i++;
}
else if ( array[i] > array2.array[j])
{
result.append(array2.array[j]);
j ++;
}
else
{
i ++;
j ++;
}
}
for ( ; i < length;i++)
result.append(array[i]);
for( ; j< array2.length; j++)
result.append(array2.array[j]);
return result;
}
Array Intersection_sorted(Array &array2)
{
//create the target Array object
Array result;
result.size = size + array2.size;
int *a = new int[size];
result.array = a;
int i = 0, j = 0;
while ( i < length && j < array2.length)
{
if( array[i] < array2.array[j])
i++;
else if( array[i] > array2.array[j])
j++;
else
{
result.append(array[i]);
i++;
j++;
}
}
return result;
}
Array Intersection_unsorted(Array &array2)
{
//create the target Array object
Array result;
result.size = size + array2.size;
int *a = new int[size];
result.array = a;
for (int i = 0 ; i < length ; i++)
{
for ( int j = 0 ; j < array2.length ; j ++ )
{
if ( array[i] == array2.array[j])
{
result.append(array[i]);
break;
}
}
}
return result;
}
};
/* cout<< overloading*/
ostream& operator<<(ostream &os, const Array &arr)
{
for ( int i = 0; i < arr.length;i++ )
os<<arr.array[i]<<";";
return os;
}
int main()
{
struct Array myarr;
myarr.array = new int[10];
myarr.size = 10;
myarr.length = 0;
myarr.append(10);
myarr.append(11);
myarr.append(12);
myarr.append(13);
myarr.append(14);
myarr.insert(3,999);
myarr.insert(4,999);
myarr.display();
cout<<endl;
cout<<"Position"<<myarr.search_linear(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
cout<<"Position"<<myarr.search_transposition(14)<<endl;
struct Array myarr2;
myarr2.array = new int[10];
myarr2.size = 10;
myarr2.length = 0;
myarr2.append(10);
myarr2.append(11);
myarr2.append(12);
myarr2.append(13);
myarr2.append(14);
myarr2.insert(3,999);
myarr2.insert(4,999);
myarr2.remove(0);
myarr2.remove(3);
myarr2.remove(5);
myarr2.display();
cout<<endl;
cout<<myarr2.search_binary(999)<<endl;
cout<<myarr2<<endl;
cout<<myarr2.sum()<<endl;
cout<<myarr2.average()<<endl;
cout<<myarr2.Max()<<endl;
cout<<myarr2.Min()<<endl;
myarr2.reverse_cp();
cout<<myarr2<<endl;
myarr2.reverse_s();
cout<<myarr2<<endl;
myarr2.left_shift(2);
cout<<myarr2<<endl;
myarr2.right_shift(1);
cout<<myarr2<<endl;
struct Array arr3;
arr3.size = 20;
arr3.length = 0;
arr3.append(1);
arr3.append(-1);
arr3.append(-3);
arr3.append(3);
arr3.append(-10);
arr3.append(5);
arr3.append(7);
arr3.append(9);
arr3.append(-10);
arr3.append(11);
arr3.append(13);
arr3.append(-12);
arr3.append(15);
arr3.cluster();
cout<<arr3<<endl;
/*Test merge method*/
Array a1;
a1.size = 10;
a1.array = new int[10];
a1.append(-3);
a1.append(-2);
a1.append(3);
a1.append(6);
a1.append(10);
Array a2;
a2.size =10;
a2.array = new int[10];
a2.append(-4);
a2.append(1);
a2.append(5);
a2.append(7);
a2.append(8);
struct Array merged_array = a1.merge(a2);
cout<<merged_array<<endl;
Array u_arr1 = a2.Union_unsorted(a1);
cout<<u_arr1<<endl;
Array a3;
a3.size =10;
a3.array = new int[10];
a3.append(-4);
a3.append(1);
a3.append(5);
a3.append(7);
a3.append(8);
a3.append(9);
Array a4;
a4.size =10;
a4.array = new int[10];
a4.append(-3);
a4.append(-2);
a4.append(5);
a4.append(6);
a4.append(9);
Array u_arr2 = a3.Union_sorted(a4);
cout<<u_arr2<<endl;
Array d_arr1 = a3.Difference_unsorted(a4);
cout<<d_arr1<<endl;
Array d_arr2 = a3.Difference_sorted(a4);
cout<<d_arr2<<endl;
Array i_arr2 = a3.Intersection_sorted(a4);
cout<<"Intersection"<<i_arr2<<endl;
Array i_arr1 = a3.Intersection_unsorted(a4);
cout<<"Intersection"<<i_arr1<<endl;
}