01:public class LinearSearch {
02:03: public static int exec(int[] vals, int target) {
04: for(int i = 0; i < vals.length; ++i)
05: if (vals[i] == target) return i;
06: return -1;
07: }// end of exec
08:09: public static void main(String[] args) {
10: int len = 100000;
11: int[] data;
12: data = new int[len];
13: for(int i=0; i<len; ++i) data[i]=i;
14: int result;
15: int repeat = 10000;
16: long time;
17: time = System.currentTimeMillis();
18: for (int i=0; i<repeat; ++i){
19: LinearSearch.exec(data,3);
20: LinearSearch.exec(data,27);
21: LinearSearch.exec(data,(int)(len/2));
22: LinearSearch.exec(data,(int)(len*2));
23: }
24: time = System.currentTimeMillis() - time;
25: System.out.println(time + "[msec]passed.");
26: }// end of main
27:28:29: private static void resultPrint(int result){
30: if (result != -1) {
31: System.out.println("[ Found ] index key = " + result);
32: } else {
33: System.out.println("[Not Found]");
34: }
35: }// end of resultPrint
36:37:}// end of LinearSearch