Basic Algorithms

Selection Sort

In this algorithm we start at the first element of the array and go through the array and find the minimum element. We swap the minimum element with the element at the first place. We start at the second position in the array and go through the entire array and find the minimum element in the remaining portion of the array. We swap that minimum element with the element at the second position. We start at the third position and repeat the procedure until we have reached the end of the array.
public static void selectionSort ( int[] anArray )
{ 
  for ( int i = 0; i < anArray.length - 1; i++ )
  {
    // Find the minimum
    int min = anArray[i];
    int minIdx = i;
		      
    for ( int j = i + 1; j < anArray.length; j++ )
    {
      if ( anArray[j] < min )
      {
        min = anArray[j];
        minIdx = j;
      }
    }
    // Swap the minimum element with the element at the ith place
    anArray[minIdx] = anArray[i];
    anArray[i] = min;
  }
}

Binary Search

If the array is already sorted use binary search to find an element in the array. The algorithm was explained in class and this is the code for it.
public int binarySearch ( int[] a, int x )
{
  int low = 0;
  int high = a.length - 1;
  int mid;
  while ( low <= high )
  {
    mid = ( low + high ) / 2;

    if ( x > a[mid] )
      low = mid + 1;
    else if ( x < a[mid] )
      high = mid - 1;
    else
      return mid;   // x == a[mid]
  }

  // The element does not exist in the array
  return (-1);
}

Merge Routine

In this merge routine we assume that the arrays that are passed to it are of non zero length and sorted in ascending order.
public static int[] merge ( int[] arrayA, int[] arrayB )
{
  // Create an array C that will hold all the elements in arrays A and B.
  int[] arrayC = new int [ arrayA.length + arrayB.length ];

  // Create pointers that will traverse the arrays. The pointers are
  // really pointing to the index of the array element that we are visiting
  int idxA = 0;
  int idxB = 0;
  int idxC = 0;

  // Compare elements in each array A and B and write out the elements in
  // array C
  while ( ( idxA < arrayA.length ) && ( idxB < arrayB.length ) )
  {
    if ( arrayA [idxA] < arrayB [idxB] )
    {
      arrayC [idxC++] = arrayA [idxA++];
    }
    else
    {
      arrayC [idxC++] = arrayB [idxB++];
    }
  }

  // Write out array A if it is not empty
  while ( idxA < arrayA.length )
  {
    arrayC [idxC++] = arrayA [idxA++];
  }

  // Write out array B if it is not empty
  while ( idxB < arrayB.length )
  {
    arrayC [idxC++] = arrayB [idxB++];
  }

  return arrayC;
}

Class Arrays

In the java.util package there is a class called Arrays that contains various methods for manipulating arrays. The methods that you will find useful are: I have appended a piece of code that shows how to use the methods in this class. Do not forget to use the import statement in your program.

import java.util.*;

public class TestArray
{
  // This method prints an array
  public static void printArray ( int[] anArray )
  {
    for ( int i = 0; i < anArray.length; i++ )
      System.out.print ( anArray[i] + "  " );
    System.out.println ();
  }

  public static void main ( String [] args )
  {
    // Create an array and print it
    int[] intArray = { 87, 23, 67, 46, 91, 52 };
    printArray ( intArray );

    // Sort the array and print it
    Arrays.sort ( intArray );
    printArray ( intArray );

    // Search for an item in the array
    int x = 46;
    int i = Arrays.binarySearch ( intArray, x );
    System.out.println ( "The number " + x + " is at " + i );

    // Search for an item not in the array
    x = 73;
    i = Arrays.binarySearch ( intArray, x );
    System.out.println ( "The number " + x + " is at " + i );

    // Create another array and test if they are equal
    int[] intArray2 = { 87, 67, 23, 46, 91, 52 };
    if ( Arrays.equals ( intArray, intArray2 ) )
      System.out.println ( "The arrays are equal.");
    else
      System.out.println ( "The arrays are not equal." );

    // Print the array, fill it with a specified value and print it
    // again to verify
    printArray ( intArray2 );
    Arrays.fill ( intArray2, -1 );
    printArray ( intArray2 );
  }
}