Created
May 5, 2012 20:16
-
-
Save anonymous/2605302 to your computer and use it in GitHub Desktop.
timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class searchingEvaluation{ | |
public static int linerSearch( String array[],String word,long resultsArray[]) | |
{ | |
int comparisons = 0; | |
int pos = -1; | |
//i have started the timer where the search actualy starts | |
long start = System.nanoTime(); | |
for(int i = 0;i< array.length;i++){ | |
comparisons = comparisons + 1; | |
if(array[i].equals(word)){ | |
pos = i; | |
break; | |
} | |
} | |
long stop = System.nanoTime(); | |
long total = stop - start; | |
resultsArray[0] = total; | |
resultsArray[1] = (long)(long)array.length; | |
resultsArray[2]= (long)(long)comparisons; | |
return pos; | |
} | |
public static int binarySearch(String[] array, String word,long resultsArray[]) { | |
int start = 0; | |
int end = array.length - 1;; | |
int midPt; | |
int pos = -1; | |
int comparisons2 = 0; | |
long start2 = System.nanoTime(); | |
Arrays.sort(array); | |
while (start <= end ) { | |
midPt = (start + end) / 2; | |
comparisons2 = comparisons2 + 1; | |
if (array[midPt].equalsIgnoreCase(word)) { | |
pos = midPt ; | |
break; | |
} | |
else if (array[midPt].compareToIgnoreCase(word) < 0) { | |
start = midPt + 1; | |
comparisons2 = comparisons2 + 1; | |
//camparisons2 addition was added inside this elseif and other elseif as a work around for not breaking the elseif statement tree,if it has made it two the last elseif then two camparisons after the first one will have been done | |
} else if (array[midPt].compareToIgnoreCase(word) > 0) { | |
comparisons2 = comparisons2 + 2; | |
end = midPt - 1; | |
} | |
} | |
long stop2 = System.nanoTime(); | |
long total2 = stop2 - start2; | |
resultsArray[0] = total2; | |
resultsArray[1] = (long)(long)array.length; | |
resultsArray[2]= (long)(long)comparisons2; | |
return pos; | |
} | |
public static void main(String [] args) | |
{ | |
Scanner input = new Scanner (System.in); | |
System.out.println("Enter Filename"); | |
String filename = input.nextLine(); | |
try { | |
Scanner in = new Scanner(new File(filename)); | |
String[] words; | |
//note,i did not change the nubmer at the start of test_sorted_large as i found a different solution to the problem,always using nextLine and trim | |
String length1 = in.nextLine().trim(); | |
int length2 = Integer.parseInt(length1); | |
words = new String[length2]; | |
for(int i =0;i < words.length;i ++){ | |
words[i] = in.nextLine().trim(); | |
} | |
long[] resultsLinearSearch; | |
resultsLinearSearch =new long[3]; | |
int poss =linerSearch(words,"Rainbow",resultsLinearSearch); | |
System.out.println( "These are the results for Linear Search"); | |
System.out.println( " found in position(starting at 0) " + poss); | |
System.out.println("The linear search algorithm took this amount of time in nanoseconds:" + resultsLinearSearch[0]); | |
System.out.println("The array had this many elements:" + resultsLinearSearch[1]); | |
System.out.println("The linear search algorithm made this amount of comparisons" + resultsLinearSearch[2]); | |
long[] resultsBinarySearch; | |
resultsBinarySearch =new long[3]; | |
int poss2 =binarySearch(words,"Rainbow",resultsBinarySearch); | |
System.out.println( "These are the results for Binary Search"); | |
System.out.println( " found in position(starting at 0) " + poss2); | |
System.out.println("The binary search algorithm took this amount of time in nanoseconds:" + resultsBinarySearch[0]); | |
System.out.println("The array had this many elements:" + resultsBinarySearch[1]); | |
System.out.println("The linear search algorithm made this amount of comparisons" + resultsBinarySearch[2]); | |
} | |
catch (FileNotFoundException e){ | |
System.out.println("That file was not found. Program terminating..."); | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment