Java Lookup Algorithm (ii): Sequential lookup

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.