4.5 Implementing Array Algorithms

9 阅读2分钟

1. Exam Points

  • Implement algorithms on 1D arrays using loops (for, while, for-each, nested loops).
    • determine a minimum or maximum value
    • compute a sum or average
    • determine if at least one element has a particular property (Ex. at least one even number)
    • determine if all elements have a particular property (Ex. if all are even)
    • count the number of elements having a particular property (Ex. count even numbers)
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements
    • reverse the order of the elements

2. Knowledge Points

(1) Determine a minimum or maximum value

  • Logic: set max or min to the first element in the array, then compares max or min with subsequent elements to check if there is greater or lesser element, if there is, update max or min.
  • Example:
    int[] nums = { 10, 32, 6, 9, 11, 23, 41, 21 };
    
    // 1. find the minimum element in an array
    int min = nums[0]; // or min = Integer.MAX_VALUE
    
    for (int i = 1; i < nums.length; i++) {
        if (nums[i] < min) {
            min = nums[i];
        }
    }
    
    System.out.println("minimum:" + min);
    
    // 2. find the maximum element in an array
    int max = nums[0]; // or max = Integer.MIN_VALUE
    
    for (int i = 1; i < nums.length; i++) {
        if (nums[i] > max) {
            max = nums[i];
        }
    }
    
    System.out.println("maximum:" + max);
    

(2) Compute a sum or average

  • Logic for sum: set sum to 0 as the initial value, add the value of each element to sum.
  • Logic for average: compute the sum first, and divide sum by number of elements.
  • Example:
    int[] nums = { 10, 32, 6, 9, 11, 23, 41, 21 };
    
    // 1. find the sum of all elements in an array
    double sum = 0;
    for (int i = 0; i < nums.length; i++) {
        sum += nums[i];
    }
    
    System.out.println("sum:" + sum);
    
    // 2. find the average of all elements in an array
    
    double average = sum / nums.length;
    System.out.println("average:" + average);
    

(3) count the number of elements having a particular property

  • Logic: traverse all the elements in an 1D array, use if to check the condition, and increment count if the condition is met.
  • Example: count even numbers.
    
    int[] nums = { 10, 32, 6, 9, 11, 23, 41, 21 };
    
    // count even numbers
    
    int count = 0;
    
    for (int x : nums) {
        if (x % 2 == 0) {
            count++;
        }
    }
    
    System.out.println("count:" + count);
    
    

(4) Determine if all elements have a particular property

  • Logic: set the boolean flag to true as its initial value, and update it to false if any of the element traversed fails to meet the condition.
  • Example:
    
    // check if all the numbers are even
    
    boolean allEven = true;
    
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] % 2 != 0) {
            allEven = false;
        }
    }
    

(5) determine the presence or absence of duplicate elements

  • Logic for checking presence: Traverse the first to the second-last elements, and compare each element with all its subsequent elements, if any duplicate elements pair is found, return true. And return false if the loop is over and no duplicate elements pair is found.
  • For example, if there are 4 elements in one 1D array, compare the 1st with the 2nd, 3rd, 4th; compare the 2nd with the 3rd, 4th; compare the 3rd with the 4th.
  • Example: checking if there are duplicate elements in an 1D array.
    public static boolean hasDuplicate(int[] array) {
    
    // traverse the first to the second-last elements
    for (int i = 0; i < array.length - 1; i++) {
    
        // compare the current element with subsequent elements, so j = i + 1
        for (int j = i + 1; j < array.length; j++) {
            if (array[j] == array[i]) // if any duplicate elements pair is found
                return true; 
            }
        }
        
        // if the loop is over, and no duplicate elements pair is found, return false.
        return false;
    }
    
    public static void main(String[] args) {
    
        int[] nums = { 10, 32, 6, 9, 11, 23, 41, 21 };
    
        // determine if there are duplicate elements in the array
        
        System.out.println(hasDuplicate(nums));
    }
    

(6) Reverse elements

  • Logic: change the element of index i with the element of index length-1-i.

    • Note: you need to use an extra variable for swapping two elements.
    • Note: index is from 0 to (nums.length / 2) - 1, since you just need to swap the first half with the second half.
  • Example:

    // reverse elements
    
    int[] nums = { 1, 2, 3, 4, 5 };
    
    for (int i = 0; i < nums.length / 2; i++) {
        int temp = nums[i];
        nums[i] = nums[nums.length - 1 - i];
        nums[nums.length - 1 - i] = temp;
    }
    

3. Exercises