What is you observation after running this code? Post your results including Operating System, Hardware info, and Java version that you've used.
public class Array_PerformanceTest{
public static void main(String args[])throws java.io.FileNotFoundException,InterruptedException{
final int arraySize=Integer.parseInt(args[0]);
int testArray[]=new int[arraySize];
System.out.printf("All arrays use size: %,d",arraySize);
System.out.println(" hr: m: s: ms");
long time=System.currentTimeMillis();
for(int index=0;index<arraySize;index++)
testArray[index]=index;
System.out.printf("Initializing array time: %20s\n",(getTime(System.currentTimeMillis()-time)));
time=System.currentTimeMillis();
int result=sequentialSearch(testArray,arraySize-1);
System.out.printf(" Sequential search time: %20s\n",(getTime(System.currentTimeMillis()-time)));
time=System.currentTimeMillis();
binarySearch(testArray,arraySize-1);
System.out.printf(" Binary search time: %20s\n",(getTime(System.currentTimeMillis()-time)));
java.util.Random rand=new java.util.Random();
for(int index=0;index<arraySize;index++)
testArray[index]=rand.nextInt(arraySize);
time=System.currentTimeMillis();
selectionSort(testArray);
System.out.printf(" Selection sort time: %20s\n",(getTime(System.currentTimeMillis()-time)));
java.util.ArrayList<String> list = new java.util.ArrayList<String>(arraySize);
time=System.currentTimeMillis();
for(int index=0;index<arraySize;index++)
list.add(0, "Mary"+rand.nextInt(arraySize));
System.out.printf(" Arraylist fill time: %20s\n",(getTime(System.currentTimeMillis()-time)));
System.out.println();
}
private static int sequentialSearch(int[] array, int value){
int index,element;
boolean found; // Flag indicating search results
index = 0;
element = -1;
found = false;
while (!found && index < array.length) {
if (array[index] == value) {
found = true; // Indicate the value is found.
element = index; // Save the subscript of the value.
}
index++;
}
return element;
}
private static void selectionSort(int[] array){
int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (array.length-1); startScan++) {
minIndex = startScan;
minValue = array[startScan];
for(index = startScan + 1; index < array.length; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
private static int binarySearch(int[] array, int value){
int first,last,middle,position;
boolean found; // Flag
first = 0;
last = array.length - 1;
position = -1;
found = false;
while (!found && first <= last) {
middle = (first + last) / 2; // Calculate mid point
if (array[middle] == value) {
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}
private static String getTime(long millis){
long second = (millis / 1000) % 60;
long minute = (millis / (1000 * 60)) % 60;
long hour = (millis / (1000 * 60 * 60)) % 24;
return String.format("%02d:%02d:%02d:%04d", hour, minute, second, millis);
}
}
No comments:
Post a Comment