-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.c
More file actions
501 lines (421 loc) · 10.3 KB
/
Array.c
File metadata and controls
501 lines (421 loc) · 10.3 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
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// ARRAY ADT
// AUTHOR: Mert Eldemir
// This is array modify console aspplication. Run the application and perform various methods on Array.
#define DISPLAY 1
#define APPEND 2
#define INSERT 3
#define DELETE 4
#define SEARCH 5
#define GET 6
#define SET 7
#define MAX 8
#define MIN 9
#define SUM 10
#define AVG 11
#define REVERSE 12
#define LEFT_SHIFT 13
#define RIGHT_SHIFT 14
#define ROTATE 16
#define IS_SORTED 17
#define EXIT 0
void swap(int *x, int *y);
struct Array
{
int *A;
int size;
int length;
};
// Optional: Resizing for dynamic array implementation
void Resize(struct Array *arr)
{
arr->size *= 2;
int *temp = (int *)realloc(arr->A, arr->size * sizeof(int));
if (temp == NULL)
{
printf("**Error: Memory reallocation failed!");
free(arr->A);
exit(1);
}
arr->A = temp;
}
void Display(struct Array *arr) // Time: O(n)
{
printf("Array: [ ");
for (int i = 0; i < arr->length; ++i)
printf("%d ", arr->A[i]);
printf("]\n");
}
void Append(struct Array *arr, int num) // Time: O(1)
{
if (arr->size == arr->length)
{
printf("\t\t** Error: No empty space to Append");
return;
}
arr->A[arr->length] = num;
++arr->length;
}
void Insert(struct Array *arr, int index, int num) // Time Worst: O(n) Best: O(1)
{
if (arr->size == arr->length)
{
printf("\t\t** Error: No empty space to Insert\n");
return;
}
if (index < 0 || index > arr->length)
{
printf("\t\t** Error: Invalid index. Index should be between 0 and %d\n", arr->length);
return;
}
if (index == arr->length)
{
Append(arr, num);
return;
}
for (int i = arr->length; i > index; --i)
arr->A[i] = arr->A[i - 1];
arr->A[index] = num;
++arr->length;
}
int Delete(struct Array *arr, int index) // Time Worst: O(n) Best: O(1)
{
if (index < 0 || index >= arr->length)
{
printf("\t\t** Error: Invalid index for Delete\n");
return -1;
}
int x = arr->A[index];
for (int i = index; i < arr->length - 1; ++i)
{
arr->A[i] = arr->A[i + 1];
}
--arr->length;
arr->A[arr->length] = 0;
return x;
}
int Search(struct Array *arr, int key) // Time Worst: O(n) Best: O(1)
{
/*
Suggestions for Improving Linear Search:
1. Transposition (moving element i-1 index on each search)
2. Mo to Front (moving element to 0 index)
3. Two Pointer Approach (in cases when chance of key on the end is same as on beginning)
4. Binary Search (has O(logn) time complexity for sorted elements)
*/
for (int i = 0; i < arr->length; ++i)
{
if (arr->A[i] == key)
{
return i;
}
}
return -1;
}
int Get(struct Array *arr, int index) // Time: O(1)
{
if (index < 0 || index >= arr->length)
{
printf("Error: Invalid index to Get\n");
return -1;
}
return arr->A[index];
}
int Set(struct Array *arr, int index, int num) // Time: O(1)
{
if (index < 0 || index >= arr->length)
{
printf("Error: Invalid index to Set\n");
return -1;
}
return arr->A[index] = num;
}
int Max(struct Array *arr) // Time: O(n)
{
if (arr->length == 0)
{
printf("Error: Max number not found\n");
return -1;
}
int max = INT_MIN;
for (int i = 0; i < arr->length; ++i)
{
if (arr->A[i] > max)
max = arr->A[i];
}
return max;
}
int Min(struct Array *arr) // Time: O(n)
{
if (arr->length == 0)
{
printf("Error: Min number not found\n");
return -1;
}
int min = INT_MAX;
for (int i = 0; i < arr->length; ++i)
{
if (arr->A[i] < min)
min = arr->A[i];
}
return min;
}
int Sum(struct Array *arr) // Time: O(n)
{
int sum = 0;
for (int i = 0; i < arr->length; ++i)
sum += arr->A[i];
return sum;
}
float Avg(struct Array *arr) // Time: O(n)
{
return (float)Sum(arr) / arr->length;
}
void Reverse(struct Array *arr) // Time: O(n)
{
int l = 0, r = arr->length - 1;
while (l < r)
{
swap(&arr->A[l], &arr->A[r]);
++l, --r;
}
}
void LeftShift(struct Array *arr) // Time: O(n)
{
int prev = arr->A[arr->length - 1];
arr->A[arr->length - 1] = 0;
for (int i = arr->length - 2; i >= 0; --i)
{
int temp = arr->A[i];
arr->A[i] = prev;
prev = temp;
}
}
void RightShift(struct Array *arr) // Time: O(n)
{
int prev = arr->A[0];
arr->A[0] = 0;
for (int i = 1; i < arr->length; ++i)
{
int temp = arr->A[i];
arr->A[i] = prev;
prev = temp;
}
}
void Rotate(struct Array *arr, int k) // Time: O(n) Space: O(n)
{
// rotate the array to the right by k steps.
if (k < 0)
{
printf("\t\t** Error: Invalid negative step for Rotate\n");
return;
}
if (arr->length == 1 || k == 0 || k % arr->length == 0)
return;
if (arr->length == 2)
{
if (k % 2 != 0)
swap(&arr->A[0], &arr->A[1]);
return;
}
const int arrLen = arr->length;
int *temp = (int *)malloc(arrLen * sizeof(int));
for (int i = 0; i < arrLen; ++i)
temp[(i + k) % arrLen] = arr->A[i];
for (int i = 0; i < arrLen; ++i)
arr->A[i] = temp[i];
free(temp);
}
int IsSorted(struct Array *arr) // Time: O(n)
{
// non-descending order
for (int i = 0; i < arr->length - 1; ++i)
{
if (arr->A[i] > arr->A[i + 1])
return 0;
}
return 1;
}
void displayOptions()
{
printf("\t1: Display\t6: Get\t\t11: Avg\t\t17: Is Sorted\n");
printf("\t2: Append\t7: Set\t\t12: Reverse\n");
printf("\t3: Insert\t8: Max\t\t13: Left Shift\n");
printf("\t4: Delete\t9: Min\t\t14: Right Shift\n");
printf("\t5: Search\t10: Sum\t\t16: Rotate\n");
printf("0: Exit");
printf("\n");
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
struct Array arr;
int elemCount;
// Initialize Array on HEAP
printf("Enter size of an array: ");
scanf("%d", &arr.size);
if (arr.size <= 0)
{
printf("\t\t** Error: Array size must be positive\n");
return 1;
}
arr.A = (int *)malloc(arr.size * sizeof(int));
arr.length = 0;
printf("Enter number of elements: ");
scanf("%d", &elemCount);
if (elemCount > arr.size)
{
printf("\t\t** Error: Number of elements can not be greater than allocated array size\n");
free(arr.A);
return 1;
}
printf("Enter all elements: \n");
for (int i = 0; i < elemCount; ++i)
{
printf("\tElement %d: ", i + 1);
scanf("%d", &arr.A[i]);
}
arr.length = elemCount;
Display(&arr);
// Manipulatios on created Array from console
while (1)
{
printf("Choose Options on Array: \n");
displayOptions();
int option;
printf("-> ");
scanf("%d", &option);
switch (option)
{
case DISPLAY:
Display(&arr);
break;
case APPEND:
{
printf("\t\tEnter number: ");
int num;
scanf("%d", &num);
Append(&arr, num);
break;
}
case INSERT:
{
int index, num;
printf("\t\tEnter insert index: ");
scanf("%d", &index);
printf("\t\tEnter number to insert: ");
scanf("%d", &num);
Insert(&arr, index, num);
break;
}
case DELETE:
{
int index;
printf("\t\tEnter delete index: ");
scanf("%d", &index);
printf("-> Deleted value: %d\n", Delete(&arr, index));
break;
}
case SEARCH:
{
int key;
printf("\t\tEnter num to search: ");
scanf("%d", &key);
printf("-> Found num at index: %d\n", Search(&arr, key));
break;
}
case GET:
{
int index;
printf("\t\tEnter index to get: ");
scanf("%d", &index);
printf("-> Found num: %d\n", Get(&arr, index));
break;
}
case SET:
{
int index, num;
printf("\t\tEnter index to set: ");
scanf("%d", &index);
printf("\t\tEnter number to set: ");
scanf("%d", &num);
printf("-> Setted num: %d\n at index: %d\n", Set(&arr, index, num), index);
break;
}
case MAX:
{
printf("-> Max found number: %d\n", Max(&arr));
break;
}
case MIN:
{
printf("-> Min found number: %d\n", Min(&arr));
break;
}
case SUM:
{
printf("-> Sum of all elements: %d\n", Sum(&arr));
break;
}
case AVG:
{
printf("-> Average of all elements: %.2f\n", Avg(&arr));
break;
}
case REVERSE:
{
Reverse(&arr);
printf("-> Elements reversed: ");
Display(&arr);
break;
}
case LEFT_SHIFT:
{
LeftShift(&arr);
printf("-> Elements left shifted: ");
Display(&arr);
break;
}
case RIGHT_SHIFT:
{
RightShift(&arr);
printf("-> Elements right shifted: ");
Display(&arr);
break;
}
case ROTATE:
{
int k;
printf("\tEnter non-negative Rotate step: ");
scanf("%d", &k);
Rotate(&arr, k);
printf("-> Elements right roted for %d steps: ", k);
Display(&arr);
break;
}
case IS_SORTED:
{
if (IsSorted(&arr))
printf("-> Array is Sorted in non-descending order!\n");
else
printf("-> Array is NOT Sorted in non-descending order!\n");
break;
}
case EXIT:
free(arr.A);
arr.A = NULL;
return 0;
}
}
// Deallocate memory
free(arr.A);
arr.A = NULL;
return 0;
}