public class ArrayEx
{
    public static void selectionSort(int [] elements)
    {
        for(int i = 0; i < elements.length - 1; i++)
        {
            int minIndex = i;
            for(int j = i+1; j < elements.length; j++)
            {
                if(elements[j] < elements[minIndex])
                    minIndex = j;
            }

            if (minIndex != i)
            {
                int temp = elements[i];
                elements[i] = elements[minIndex];
                elements[minIndex] = temp;
            }
            System.out.println(elements[i]);
        }
        System.out.println(elements[elements.length - 1]);
    }

   public static int binarySearch(int[] list, int target)
    {   //pre: list != null, list sorted in ascending order
        int low = 0;
        int high = list.length - 1;
        int mid;
        int result = -1;
        while( result == -1 && low <= high)
        {   mid = (low + high) / 2;
            if( list[mid] == target )
                result = mid;
            else if( list[mid] < target )
                low = mid + 1;
            else
                high = mid - 1;
        }
        return result;
    }

    //pre: list != null, 0 <= start < list.length
    //post: return index of first occurence of target
    // in list equal to or greater than start, -1 if target is not present
    public static int indexOf(int[] list, int target, int start)
    {   int result = -1;
        int index = start;
        while(result == -1 && index < list.length)
        {   if( list[index] == target )
                result = index;
            index++;
        }
        return result;
    }

    // pre: sen != null
    // post: return true if every letter in
    // alphabet is present in sen at least once
    // else return false
    public static boolean validTest(String sen)
    {   String allCaps = sen.toUpperCase();
        boolean[] found = new boolean[26];
        char c;
        int index;
        for(int i = 0; i < allCaps.length(); i++)
        {   c = allCaps.charAt(i);
            index = c - 'A';
            if( index >= 0 && index < found.length)
                found[index] = true;
        }
        boolean result = true;
        int i = 0;
        while( result && i < found.length )
        {   result = found[i];
            i++;
        }
        return result;
    }

    public static boolean[] printPrimes(int limit)
    {   boolean[] numbers = new boolean[limit+1];
        for(int i = 2; i < numbers.length; i++)
            numbers[i] = true;
        double stop = Math.sqrt(limit);
        for(int i = 2; i < stop; i++)
        {   if( numbers[i] )
            {   for(int number = 2 * i; number < numbers.length; number += i)
                    numbers[number] = false;
            }
        }
        /*
        for(int i = 2; i < numbers.length; i++)
            if( numbers[i] )
                System.out.println(i);
        */
       return numbers;
    }

    public static void reverse(int[] list)
    {   int temp;
        int limit = list.length / 2;
        for(int i = 0; i < limit; i++)
        {   temp = list[i];
            list[i] = list[list.length - i - 1];
            list[list.length - i - 1] = temp;
        }
    }

    public static int[] rev(int[] list)
    {   int[] result = new int[list.length];
        for(int i = 0; i < list.length; i++)
        {   result[i] = list[list.length - i - 1];
        }
        return result;
    }


    public static int numPresent(String[] list, String target)
    {   int total = 0;
        for(int i = 0; i < list.length; i++)
        {   if(list[i].equals(target))
                total++;
        }
        return total;
    }

    public static int numPresent(String[] list, String[] target)
    {   int total = 0;
        for(int i = 0; i < target.length; i++)
        {   total += numPresent(list, target[i]);
        }
        return total;
    }

    public static void go()
    {   for(int i = 1000000; i < 10000000; i += 1000000)
        {   System.out.println(i);
            Circle[] cList = new Circle[i];
            for(int j = 0; j < cList.length; j++)
                cList[j] = new Circle();
            System.out.println( cList[2] );
        }
    }

    //pre: list != null, list.length > 0
    public static int max(int[] list)
    {   int maxSoFar = list[0];
        for(int i = 1; i < list.length; i++)
        {   if( list[i] > maxSoFar )
                maxSoFar = list[i];
        }
        return maxSoFar;
    }

    //pre: list != null, list.length > 0
    public static int min(int[] list)
    {   int minSoFar = list[0];
        for(int i = 1; i < list.length; i++)
        {   if( list[i] < minSoFar )
                minSoFar = list[i];
        }
        return minSoFar;
    }

    public static double ave(int[] list)
    {   double sum = 0;
        for(int i = 0; i < list.length; i++)
            sum += list[i];
        return sum / list.length;
    }
}
