-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathArrayUtilities.java
More file actions
317 lines (272 loc) Β· 9.16 KB
/
ArrayUtilities.java
File metadata and controls
317 lines (272 loc) Β· 9.16 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
/**
* Array Utilities
* ================
* A collection of useful array manipulation and analysis methods.
* Provides common operations for working with integer arrays.
*
* Features:
* - Find min/max values
* - Calculate sum and average
* - Reverse and sort arrays
* - Search for elements
* - Display array contents
*
* Author: Hacktoberfest 2025 Contributor
* Date: October 20, 2025
*/
public class ArrayUtilities {
/**
* Finds the minimum value in an array
*
* @param arr The input array
* @return The minimum value, or Integer.MAX_VALUE if array is empty
*/
public static int findMin(int[] arr) {
if (arr == null || arr.length == 0) {
return Integer.MAX_VALUE;
}
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
/**
* Finds the maximum value in an array
*
* @param arr The input array
* @return The maximum value, or Integer.MIN_VALUE if array is empty
*/
public static int findMax(int[] arr) {
if (arr == null || arr.length == 0) {
return Integer.MIN_VALUE;
}
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
/**
* Calculates the sum of all elements in an array
*
* @param arr The input array
* @return The sum of all elements
*/
public static int calculateSum(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
/**
* Calculates the average of all elements in an array
*
* @param arr The input array
* @return The average as a double, or 0.0 if array is empty
*/
public static double calculateAverage(int[] arr) {
if (arr == null || arr.length == 0) {
return 0.0;
}
return (double) calculateSum(arr) / arr.length;
}
/**
* Reverses an array in place
*
* @param arr The array to reverse
*/
public static void reverseArray(int[] arr) {
if (arr == null || arr.length <= 1) {
return;
}
int left = 0;
int right = arr.length - 1;
while (left < right) {
// Swap elements
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
/**
* Searches for an element in an array
*
* @param arr The array to search
* @param target The element to find
* @return The index of the element, or -1 if not found
*/
public static int linearSearch(int[] arr, int target) {
if (arr == null) {
return -1;
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
/**
* Checks if an array contains a specific element
*
* @param arr The array to search
* @param target The element to find
* @return true if element exists, false otherwise
*/
public static boolean contains(int[] arr, int target) {
return linearSearch(arr, target) != -1;
}
/**
* Sorts an array using bubble sort algorithm
*
* @param arr The array to sort
*/
public static void bubbleSort(int[] arr) {
if (arr == null || arr.length <= 1) {
return;
}
int n = arr.length;
boolean swapped;
// Bubble sort with optimization
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no swaps occurred, array is sorted
if (!swapped) {
break;
}
}
}
/**
* Creates a copy of an array
*
* @param arr The array to copy
* @return A new array with the same elements
*/
public static int[] copyArray(int[] arr) {
if (arr == null) {
return null;
}
int[] copy = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
copy[i] = arr[i];
}
return copy;
}
/**
* Displays array contents in a formatted way
*
* @param arr The array to display
* @param label Optional label for the array
*/
public static void printArray(int[] arr, String label) {
if (label != null && !label.isEmpty()) {
System.out.print(label + ": ");
}
if (arr == null) {
System.out.println("null");
return;
}
if (arr.length == 0) {
System.out.println("[]");
return;
}
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}
/**
* Main method - demonstrates array utility functions
*/
public static void main(String[] args) {
System.out.println("=".repeat(60));
System.out.println("ARRAY UTILITIES - Hacktoberfest 2025");
System.out.println("=".repeat(60));
// Test array
int[] numbers = {42, 15, 8, 23, 4, 16, 37, 11};
System.out.println("\nπ Original Array:");
printArray(numbers, "Numbers");
// Statistical operations
System.out.println("\nπ Statistical Analysis:");
System.out.println("β".repeat(40));
System.out.println("Minimum value: " + findMin(numbers));
System.out.println("Maximum value: " + findMax(numbers));
System.out.println("Sum: " + calculateSum(numbers));
System.out.printf("Average: %.2f%n", calculateAverage(numbers));
System.out.println("β".repeat(40));
// Search operations
System.out.println("\nπ Search Operations:");
System.out.println("β".repeat(40));
int searchTarget = 23;
int index = linearSearch(numbers, searchTarget);
System.out.println("Search for " + searchTarget + ": " +
(index != -1 ? "Found at index " + index : "Not found"));
int missingTarget = 99;
System.out.println("Search for " + missingTarget + ": " +
(contains(numbers, missingTarget) ? "Found" : "Not found"));
System.out.println("β".repeat(40));
// Reverse operation
System.out.println("\nπ Reverse Operation:");
int[] reversedCopy = copyArray(numbers);
reverseArray(reversedCopy);
printArray(numbers, "Original ");
printArray(reversedCopy, "Reversed ");
// Sort operation
System.out.println("\nβ¬οΈ Sort Operation:");
int[] sortedCopy = copyArray(numbers);
bubbleSort(sortedCopy);
printArray(numbers, "Original");
printArray(sortedCopy, "Sorted ");
// Combined operations example
System.out.println("\nπ― Combined Operations Example:");
System.out.println("β".repeat(40));
int[] data = {5, 2, 8, 1, 9, 3, 7, 4, 6};
printArray(data, "Original data");
bubbleSort(data);
printArray(data, "After sorting");
reverseArray(data);
printArray(data, "After reversing");
System.out.println("Min: " + findMin(data) + ", Max: " + findMax(data));
System.out.println("β".repeat(40));
// Usage examples
System.out.println("\n\nπ‘ Usage Examples:");
System.out.println("=".repeat(60));
System.out.println("\n// Find minimum value:");
System.out.println("int min = ArrayUtilities.findMin(myArray);");
System.out.println("\n// Calculate average:");
System.out.println("double avg = ArrayUtilities.calculateAverage(myArray);");
System.out.println("\n// Sort an array:");
System.out.println("ArrayUtilities.bubbleSort(myArray);");
System.out.println("\n// Search for element:");
System.out.println("int index = ArrayUtilities.linearSearch(myArray, 42);");
System.out.println("\n// Reverse an array:");
System.out.println("ArrayUtilities.reverseArray(myArray);");
System.out.println("\n" + "=".repeat(60));
System.out.println("β
Array Utilities Ready!");
System.out.println("=".repeat(60));
}
}