Some Basic Array Algorithms

// This program demonstrates various array algorithms as methods
//     make a copy of an array
//     compare two arrays for equality (see if they contain the exact same values
//     sequential search: check whether a given value is in the array
//     return highest value in array
//     return INDEX (subscript) of highest value in array

public class ArrAlgorithms {
   public static void main(String [] args) {
      int[] test1 = {85, 75, 99, 100, 15};
      int[] test2 = {85, 77, 99, 100, 15};
      int[] test3 = {85, 75, 99, 100, 15, 45, 50};
        int[] arrcopy;
      int index;
      int loc;
      int num;
      boolean result;


      // copy an array
      arrcopy = copyArray(test1);
      System.out.println("The copy of test1 is:");
      for (index = 0 ; index < arrcopy.length ; index++)
         System.out.println(arrcopy[index]);


      // compare two arrays for equality
      result = CompareArrays(test1,test2);
      if (result)
         System.out.println("test1 and test2 are the same.");
      else
         System.out.println("test1 and test2 are not the same.");


      // compare two arrays for equality
      result = CompareArrays(test1,test3);
      if (result)
         System.out.println("test1 and test3 are the same.");
      else
         System.out.println("test1 and test3 are not the same.");


      // sequential search: find out if array contains value 99
      loc = sequentialSearch(test1,99);
      if (loc == -1)
         System.out.println("99 not found in the array");
      else
         System.out.println("99 found in the array in location "
                            + (loc + 1));


      // sequential search: find out if array contains value 8
      loc = sequentialSearch(test1,8);
      if (loc == -1)
         System.out.println("8 not found in the array");
      else
         System.out.println("8 found in the array in location "
                            + (loc + 1));


      // return highest value in array
      num = max(test1);
      System.out.println("The highest value in the array is " + num);


      // return location of highest value in array
      loc = maxLoc(test1);
      System.out.println("The highest value in the array is " + test1[loc]
                          + ", which is at index " + loc);

   }  // end of main



    // copy an array
    public static int[] copyArray(int[] arr) {
       int i;
       int[] copy = new int[arr.length];

       for (i = 0 ; i < arr.length ; i++)
          copy[i] = arr[i];
       return copy;
    }


   // compare two arrays
   public static boolean CompareArrays(int[] array1, int[] array2) {
      int index;

      if (array1.length != array2.length)
         return false;
      for(index = 0 ; index < array1.length ; index++)
         if (array1[index] != array2[index])
            return false;
      return true;
   }


   // search for a value in an array
   public static int sequentialSearch(int[] array, int value) {
      int index;

      for(index = 0 ; index < array.length ; index++)
         if (array[index] == value)
            return index;
      return -1;
   }


   // find the highest value in an array
   public static int max(int [] array) {
      int index = 0;
      int highest;
      int i;
      highest = array[0];
      for (i = 1 ; i < array.length ; i++)
         if (array[i] > highest) {
            highest = array[i];
         }
      return highest;
   }


   // find the index of the highest value in an array
   public static int maxLoc(int [] array) {
      int index = 0;
      int highest;
      int i;
      highest = array[0];
      for (i = 1 ; i < array.length ; i++)
         if (array[i] > highest) {
            index = i;
            highest = array[i];
         }
      return index;
   }
}

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013