[What is sequential lookup]
Sequential lookup, also called Linear lookup, is the most basic search technique, which is the search process: starting with the first or last record in the table, comparing each value with the given values, such as equality, finding success, and not finding the records found in the table until the last value is still unequal, and the lookup is unsuccessful.
[Java Implementation Order Lookup]
public class Sequentialsearch {public static void main (string[] args) {Integer target = 6;integer[] Iarr = {3, 2, 6, 8, 5 , 1, 7, 9};integer index = Sequentialsearch (Iarr, target); SYSTEM.OUT.PRINTLN (index);} public static <T> Integer Sequentialsearch (t[] t, T-target) {for (int i = 0; T! = null && i < t.length; I + +) {if (t[i] = = target) {return i;}} return-1;}}
[Pros and cons of sequential lookups]
Pros: Code is easy to understand
Disadvantage: When the data volume is large, the search efficiency is very low, so the algorithm is suitable for a small amount of data.
[Algorithmic complexity of sequential lookups]
The best case for finding success is found in the first position, the algorithm time complexity is O (1)
The worst case scenario is found in the last position, with the time complexity O (n)
The probability of the keyword is the same at any one location, so the average number of lookups is (n+1)/2, so the final time complexity is O (n)
Java Lookup Algorithm (ii): Sequential lookup