Sequential lookups are also called linear lookups:
Basic idea: From one end of the lookup table, to the other end by the given value of K and the keyword to compare, if found, find success;
and gives the position of the record in the table, if the whole table is detected, the keyword with the same k value is still not found, then the lookup fails;
Advantages: There is no requirement for the storage of data in the table, and it can only be searched sequentially for the linked list;
Disadvantage: When the value of n is large, the average search length is large and the efficiency is low.
Import Java.util.Scanner;
public class Ordersearch {
/**
* Sequential Lookup algorithm
*
* @param Srcarray ordered array
* @param key to find elements
* @return Key array subscript, not found return-1
*/
public static void Main (string[] args) {
Int[] Srcarray = {3,5,11,17,21,23,28,30,32,50,64,78,81,95,101};
System.out.println ("Please enter the number to look for:");
Scanner input=new Scanner (system.in);
int Key=input.nextint ();
Input.close ();
SYSTEM.OUT.PRINTLN ("Sequential lookup algorithm results:" +ordersearch (Srcarray,key));
}
private static int Ordersearch (int[] srcarray, int key) {
for (int i=0;i<srcarray.length;i++) {
if (Srcarray[i]==key) {
return i;
}
}
return-1;
}
}
Sequential Lookup Java implementations