/*
 * This class will be used to work with arrays of salaries
 * @author Jeff Borland(change)
 * @date 3-18-15
 */
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySearching
{
	private final int NUM_OF_VALUES=5000000;  
	private int[] allNums = new int [NUM_OF_VALUES];
	private Scanner scan;  
	private static long start;
	private static long finish;   

	/*
	 * The constructor below will automatically read in all of the Baseball player salaries
	 */
	public ArraySearching() 
	{

		reset(); //resets the scanner to the beginning of the file
		int i=0;
		while (scan.hasNext() && i<NUM_OF_VALUES)
		{
			String theLine=scan.nextLine(); //This reads in the next player line                           
			int Num=Integer.parseInt(theLine);  //converts the string Num to an integer
			allNums[i]=Num;
			i++;                       
		}
		System.out.println(allNums[4386612]+","+allNums[2453472]+","+allNums[4121824]+","+allNums[139856]+","+allNums[3898511]+","+allNums[1455895]);

	}

	public void sort()
	{
		Arrays.sort(allNums); 
	}


	public static void startClock()
	{     
		start = System.currentTimeMillis();   
	}
	public static void stopClock()
	{
		finish= System.currentTimeMillis();   
		System.out.println( "This operation took " + (finish-start) + " milliseconds.");
	}




	//This method will reset your scanner to beginning of file
	//note it is private - why?
	private void reset()
	{
		//To reset your scanner:
		try
		{
			scan=new Scanner(new File("list.txt"));
		}
		catch (IOException e){}
	}

	public boolean findNum(int numToCheck) 
	{    	
		for(int i=0; i<allNums.length;i++)
			if (allNums[i]==numToCheck)
				return true;
		return false;
	}    


	public boolean findFast(int num)
	{
		int low=0;
		int high=(allNums.length)-1;
		while (low<=high)
		{
			int mid=(low+high)/2;
			if(allNums[mid]<num)
				low=mid+1;
			else if (allNums[mid]>num)
				high=mid-1;
			else
				return true;
		}
		return false;
	}


	public static void main(String[] args) {
		startClock();
		System.out.println("Loading the list from the file:");
		ArraySearching a = new ArraySearching();
		stopClock();		
		startClock();
		for (int i=0;i<100;i++)
			System.out.println("1362324080 is found:"+a.findNum(1362324080));
		stopClock();



		//NOW SORT THE ARRAY TO TRY FAST FOUND
		startClock();
		System.out.println("Sorting the list with built in sort");
		a.sort();
		stopClock();

		/*startClock();
		for (int i=0;i<100;i++)
			System.out.println("1362324080 is found:"+a.findNum(1362324080));
		stopClock();*/


		startClock();
		for (int i=0;i<100;i++)
			System.out.println("584481493 is fast found:"+a.findFast(584481493));
		stopClock();


	}

}