public static double findMax(double[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("Array cannot be null or empty");
}
double max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
public static double getFirstValue(double[] array) {
double firstValue = Double.NaN;
if (array != null && array.length > 0) {
firstValue = array[0];
}
return firstValue;
}
public static void bubbleSort(double[] array) {
if (array != null && array.length > 1) {
int n = array.length;
boolean hasSwapped = true;
int pass = 0;
while (hasSwapped && pass < n - 1) {
hasSwapped = false;
for (int j = 0; j < n - 1 - pass; j++) {
if (array[j] > array[j + 1]) {
double swapHolder = array[j];
array[j] = array[j + 1];
array[j + 1] = swapHolder;
hasSwapped = true;
}
}
pass++;
}
}
}
public static void moveZeroesToEnd(double[] array) {
if (array != null) {
int insertIndex = 0;
int scoutIndex = 0;
int n = array.length;
while (insertIndex < n) {
while (scoutIndex < n && array[scoutIndex] == 0.0) {
scoutIndex++;
}
if (scoutIndex < n) {
array[insertIndex] = array[scoutIndex];
scoutIndex++;
} else {
array[insertIndex] = 0.0;
}
insertIndex++;
}
}
}
public static int binarySearch(double[] array, double target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
// Calculates mid point, prevents integer overflow for large arrays
int mid = left + (right - left) / 2;
if (array[mid] < target) {
left = mid + 1; // Target is in the right half
} else if (array[mid] > target) {
right = mid - 1; // Target is in the left half
} else {
return mid; // Target found, return its index
}
}
return -1; // Target not found
}
public static void reverseArray(double[] array) {
if (array != null && array.length > 1) {
int left = 0;
int right = array.length - 1;
while (left < right) {
double swapHolder = array[left];
array[left] = array[right];
array[right] = swapHolder;
left++;
right--;
}
}
}
public static boolean hasDuplicate(double[] array) {
boolean duplicateFound = false;
if (array != null && array.length > 1) {
int i = 0;
int n = array.length;
while (i < n - 1 && !duplicateFound) {
int j = i + 1;
while (j < n && !duplicateFound) {
if (array[i] == array[j]) {
duplicateFound = true;
}
j++;
}
i++;
}
}
return duplicateFound;
}
public static int longestIdenticalRun(double[] array) {
int maxRun = 0;
if (array != null && array.length > 0) {
int i = 0;
int n = array.length;
while (i < n) {
int currentRun = 1;
int j = i + 1;
while (j < n && array[j] == array[i]) {
currentRun++;
j++;
}
if (currentRun > maxRun) {
maxRun = currentRun;
}
i = j;
}
}
return maxRun;
}
This page was last modified on 2026-09-27 at 21:40:03.
George Fox University · 414 N Meridian St · Newberg, Oregon 97132 · 503-538-8383
Copyright © 2018–2026 George Fox University. All rights reserved.