# 查找——顺序查找

public static int seqSearch01(Object[] a, Object x, int n){
int i;
for (i = 0; i < n; i++) {
if(a[i].equals(x))
break;
}
if(i<n)
return i;
return -1;
}

public static int seqSearch02(Object[] a, Object x, int n){
a[n]=x;
int i;
for (i = 0;; i++) {
if(a[i].equals(x))
break;
}
if(i<n)
return i;
return -1;
}

private static final int MAX_LENGTH = 10000;
private static final int N=MAX_LENGTH-1;

static Object[] a = new Object[MAX_LENGTH];

static{
for(int i=0;i<MAX_LENGTH;i++){
a[i]=i+1;
}
}

public static void seqSearch(Object x){
System.out.println("===========顺序查找============");
// 顺序查找01
long b = System.nanoTime();
int m = SequentialSearch.seqSearch01(a, x, N);
System.out.println("数组元素的下标为：" + m);
long time01 = System.nanoTime()-b;
// 顺序查找02
b=System.nanoTime();
m = SequentialSearch.seqSearch02(a, x, N);
System.out.println("数组元素的下标为：" + m);
long time02 = System.nanoTime()-b;
System.out.println("时间差值为："+(time01-time02)+"μs");
}

01-14 2818
05-30 40

07-20 111
09-15 856
12-15 1666
01-23 311
04-02 41
12-05 5788