-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArrayList.h
257 lines (239 loc) · 4.85 KB
/
ArrayList.h
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
#include <limits>
class ArrayList
{
private:
int *arr;
int size;
int ind;
int partition(int low, int high);
int lcmUtility(int num1, int num2);
int gcdUtility(int num1, int num2);
void swap(int *num1, int *num2);
void sorter(int low, int high);
public:
ArrayList(){
this->size = 0;
this->arr = new int[this->size];
this->ind = 0;
}
int sum();
void add(int data);
void reverse();
int search(int k);
void sort();
int min();
int max();
int frequency(int k);
int lcmOfArray();
int gcdOfArray();
void printArray();
bool remove(int data_to_remove);
};
/*
Finding the sum of the elements in the array
Returns the sum of the array
*/
int ArrayList::sum()
{
int array_sum = 0, index;
for (index = 0; index < this->size; index++)
array_sum += this->arr[index];
return array_sum;
}
/*
Reverse the array
The Reverse operation happens in place
*/
void ArrayList::reverse()
{
int index, safe;
for (index = 0; index < this->size / 2; index++)
{
safe = this->arr[index];
this->arr[index] = this->arr[(this->size) - index - 1];
this->arr[(this->size) - index - 1] = safe;
}
}
/*
Add a new item to the ArrayList
*/
void ArrayList::add(int data)
{
this->size++;
this->arr[this->ind++] = data;
}
/*
Prints the array
*/
void ArrayList::printArray()
{
for(int tempInd = 0; tempInd<this->size ; tempInd++)
std::cout<<this->arr[tempInd]<<" ";
std::cout<<std::endl;
}
/*
Removes the element from the ArrayList
*/
bool ArrayList::remove(int data_to_remove)
{
int tempInd;
for(tempInd =0; tempInd<this->ind; tempInd++)
{
if(this->arr[tempInd] == data_to_remove){
break;
}
}
if(tempInd < this->size){
(this->size)--;
for(int delInd = tempInd; delInd<this->size; delInd++ ){
this->arr[delInd] = this->arr[delInd+1];
}
return true;
}
return false;
}
/*
searching for an element in the array
Returns the index where the element is found, returns -1 if not found
*/
int ArrayList::search(int k)
{
int first = 0;
int last = this->size - 1;
while (first <= last)
{
int mid = (first + last) / 2;
if (arr[mid] == k)
return mid;
else if (this->arr[mid] > k)
last = mid - 1;
else if (this->arr[mid] < k)
first = mid + 1;
}
return -1;
}
/*
Swaps two elements
*/
void ArrayList::swap(int *num1, int *num2)
{
int safe;
safe = *num1;
*num1 = *num2;
*num2 = safe;
}
/*
Helper function for sorting
Partitions according to the pivot element
*/
int ArrayList::partition(int low, int high)
{
int pivot = this->arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (this->arr[j] <= pivot)
{
i++;
swap(&(this->arr[i]), &(this->arr[j]));
}
}
swap(&(this->arr[i + 1]), &(this->arr[high]));
return (i + 1);
}
/*
Sorts the array
*/
void ArrayList::sorter(int low, int high)
{
if (low < high)
{
int pos = partition(low, high);
sorter(low, pos - 1);
sorter(pos + 1, high);
}
}
void ArrayList::sort()
{
sorter(0, this->size - 1);
}
/*
Returns the minimum element from the array
*/
int ArrayList::min()
{
int minimum = INT_MAX, index;
for (index = 0; index < this->size; index++)
{
if (this->arr[index] < minimum)
minimum = this->arr[index];
}
return minimum;
}
/*
Returns the maximum element from the array
*/
int ArrayList::max()
{
int maximum = INT_MIN, index;
for (index = 0; index < this->size; index++)
{
if (this->arr[index] > maximum)
maximum = this->arr[index];
}
return maximum;
}
/*
Returns the frequency of an element in an array
*/
int ArrayList::frequency(int k)
{
int count = 0, index;
for (index = 0; index < this->size; index++)
{
if (this->arr[index] == k)
count++;
}
return count;
}
/*
Returns the HCF of two numbers
*/
int ArrayList::gcdUtility(int num1, int num2)
{
if (num2 == 0)
return num1;
else
gcdUtility(num2, num1 % num2);
}
/*
Returns the LCM of two numbers
*/
int ArrayList::lcmUtility(int num1, int num2)
{
return (num1 * num2) / gcdUtility(num1, num2);
}
/*
Returns the HCF of the entire array
*/
int ArrayList::gcdOfArray()
{
int safe = gcdUtility(this->arr[0], this->arr[1]), index;
for (index = 2; index < this->size; index++)
{
safe = gcdUtility(this->arr[index], safe);
}
return safe;
}
/*
Returns the LCM of the entire array
*/
int ArrayList::lcmOfArray()
{
int safe = lcmUtility(this->arr[0], this->arr[1]), index;
for (index = 2; index < this->size; index++)
{
safe = lcmUtility(this->arr[index], safe);
}
return safe;
}