-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.c
150 lines (132 loc) · 3.23 KB
/
array.c
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
/* My Array Implementation */
#include "array.h"
#include <assert.h>
struct s_array {
int* array;
int size;
};
/* INIT ARRAY OBJECT */
MyArray* init()
{
MyArray* arr = malloc(sizeof(*arr));
arr->size = 0;
arr->array = NULL;
return arr;
}
/* RESIZE ARRAY TO newSize */
void resize(MyArray* arr, int newSize)
{
int len = arr->size;
arr->size = newSize;
arr->array = realloc((arr->array), newSize * sizeof(*(arr->array)));
if (len < newSize)
setInRange(arr, len, newSize, 0); // If array increased in size, set new places to 0.
}
/* RETURNS CURRENT ELEM COUNT OF ARRAY */
int arraySize(MyArray* arr)
{
return arr->size;
}
/* SETS EVERY ELEMENT IN THE ARRAY TO 0 */
void setZeroes(MyArray* arr)
{
int size = arraySize(arr);
for (int i = 0; i < size; ++i)
(arr->array)[i] = 0;
}
/* ADDS ELEMENT elem TO EVERY INDEX IN RANGE [start, end) */
void setInRange(MyArray* arr, int start, int end, int elem)
{
assert(start >= 0);
if (end > arraySize(arr))
resize(arr, end);
for (int i = start; i < end; ++i)
(arr->array)[i] = elem;
}
/* SWAPS ELEM AT INDEX i WITH ELEM AT INDEX j */
void swap(MyArray* arr, int i, int j)
{
int size = arraySize(arr);
if (i < 0 || i >= size || j < 0 || j >= size) {
printf("\nINVALID INDEX.\n");
return;
}
int aux = (arr->array)[i];
(arr->array)[i] = (arr->array)[j];
(arr->array)[j] = aux;
}
/* REPLACES ELEM AT INDEX i WITH elem */
void replaceAtIndex(MyArray* arr, int i, int elem)
{
(arr->array)[i] = elem;
}
void removeAtIndex(MyArray* arr, int idx)
{
int size = arraySize(arr);
if (idx < 0 || idx >= size) {
printf("\nINVALID INDEX.\n");
return;
}
for (int i = idx; i < size - 1; ++i)
(arr->array)[i] = (arr->array)[i + 1];
resize(arr, size - 1);
}
/* REMOVES ALL INSTANCES OF elem IN ARRAY */
void removeMatches(MyArray* arr, int elem)
{
for (int i = 0; i < arr->size; ++i) {
if ((arr->array)[i] == elem)
removeAtIndex(arr, i--);
}
}
/* FREES ALL SPACE USED BY ARRAY */
void freeArray(MyArray* arr)
{
free(arr->array);
free(arr);
}
/* PRINTS ARRAY TO STDOUT */
void printArray(MyArray* arr)
{
int size = arr->size;
printf("[ ");
for (int i = 0; i < size; ++i)
printf("%d ", (arr->array)[i]);
printf("]\n");
fflush(stdout);
}
/* AUXILIARY FUNCTION FOR QUICK SORT */
void swapArr(int* arr, int i, int j)
{
int aux = arr[i];
arr[i] = arr[j];
arr[j] = aux;
}
/* AUXILIARY FUNCTION FOR QUICK SORT */
int partition(int* arr, int lft, int rgt, bool cmp(int a, int b))
{
int piv = lft;
int pive = arr[piv];
for (int i = lft + 1; i < rgt; ++i) {
if (cmp(arr[i], pive)) {
piv++;
swapArr(arr, i, piv);
}
}
swapArr(arr, lft, piv);
return piv;
}
/* AUXILIARY FUNCTION FOR QUICK SORT */
void sort(int* arr, int lft, int rgt, bool cmp(int a, int b))
{
if (lft < rgt) {
int ppiv = partition(arr, lft, rgt, cmp);
sort(arr, lft, ppiv, cmp);
sort(arr, ppiv + 1, rgt, cmp);
}
}
/* Quick Sort the array */
void quickSort(MyArray* arr, bool cmp(int a, int b))
{
sort(arr->array, 0, arr->size, cmp);
}