Complexity Analysis


Work with your assigned partner to determine the computational complexity of the following algorithms.

  1.  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;
     }
  2. public static double getFirstValue(double[] array) {
       double firstValue = Double.NaN;
    
       if (array != null && array.length > 0) {
          firstValue = array[0];
       }
    
       return firstValue;
    }
  3. 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++;
          }
       }
    }
  4. 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++;
            }
        }
    }
    
  5. 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
    }
  6. 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--;
            }
        }
    }
  7. 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;
    }
    
  8. 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.

Copyright © 2018–2026 George Fox University. All rights reserved.