Sequential Lookup Java implementations

Source: Internet
Author: User

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

Related Article

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.