-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_16.java
More file actions
66 lines (54 loc) · 2.09 KB
/
Copy pathExercise07_16.java
File metadata and controls
66 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Arrays;
/******************************************************************************
* This program creates an array of 100,000 ints and tests the speed of search
* algorithms . The values of each array element is between 0 and 1,000. After
* search is preformed the time in MS is displayed on the screen
******************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_16 {
public static void main(String[] args) {
int[] array = create100000ints();
insertionSort(array);
int key = array[(int)(Math.random()*100000)];
long startTimeLinear = System.nanoTime();
linearSearch(array, key);
long endTimeLinear = System.nanoTime();
double LinearTime = endTimeLinear - startTimeLinear;
long startTimeBinary = System.nanoTime();
Arrays.binarySearch(array, key);
long endTimeBinary = System.nanoTime();
double BinaryTime = endTimeBinary - startTimeBinary;
BinaryTime /= 1000000.0;
LinearTime /= 1000000.0;
System.out.println("LinearTime: " + LinearTime + " ms");
System.out.println("BinaryTime: " + BinaryTime + " ms");
}
public static int[] create100000ints (){
int[] array = new int[100000];
for(int i = 0; i < array.length; i++)
array[i] = (int)(Math.random()*1000);
return array;
}
public static int linearSearch(int[] list, int key){
for (int i = 0; i < list.length; i++) {
if(key==list[i])
return i;
}
return -1;
}
public static void insertionSort(int[] array){
for (int i = 1; i < array.length; i++) {
int temp = array[i];
//move elements to the right until temp find it's spot
int j;
for (j = i-1; j >= 0 && temp < array[j]; j--) {
array[j+1] = array[j];
}
//insert temp into it's spot
array[j+1] = temp;
}
}
}