Common search algorithm summary and Search Algorithm

Source: Internet
Author: User

Common search algorithm summary and Search Algorithm

1. Sequential search

Time Complexity: O (n)
Advantage: the algorithm is simple and there are no requirements for searching table records.
Disadvantage: Low Efficiency
Applicable: search for a small amount of data

 

Principle:

Find the exact location of the number that is the same as the given keyword in a known (or ordered) ordered queue. The principle is to compare the number of keywords with the number in the queue one by one from the last until the same number is found as the given keyword.

int SequenceSearch(int *array, int size, int key){    int i;    for(i=0; i<size; i++)    {        if(array[i]==key)        {            return i;        }    }    return -1;}
Int SequenceSearch (int * array, int size, int key) {int I = size-1; array [0] = key; // while (key! = Array [I]) {I --;} return I ;}

 

2. Binary Search/half-fold search

Time Complexity: O (logn)
Advantage: the search speed is fast and the average performance is good;
Disadvantage: The table to be queried must be an ordered table, and it is difficult to insert or delete the table.
Applicable: the half-fold search method is applicable to searching for ordered lists with frequent changes.

Principle:
First, assume that the elements in the table are arranged in ascending order and the keywords recorded in the middle of the table are compared with the search keywords. If the two are the same, the search is successful; otherwise, the table is divided into the first and last sub-tables by using the intermediate position record. If the keyword recorded in the middle position is greater than the search keyword, the former sub-table is further searched. Otherwise, the latter sub-table is further searched. Repeat the preceding process until you find a record that meets the conditions to make the search successful, or until the child table does not exist, the search fails.

int BinarySearch(int *array, int size, int key){    int first,last,middle;    first=0;    last =size;    while(first<=last)    {        middle = (first+last)/2;        if(array[middle] < key)            first=middle+1;        else if(array[middle] > key)            last=middle-1;        else            return middle;    }    return -1;}

 

3. Interpolation Search

Time Complexity: O (logn)

Applicable: Search tables with large data volumes and even keyword Distribution

 

Principle:

Find the word "worst" in the English-Chinese dictionary. We will never look for the elements in the middle of the dictionary, and then look for the elements at 3/4 in the dictionary. in fact, we are looking for the desired address (in the back of the dictionary.

int InsertSearch(int *array, int size, int key){    int first,last,position;    first=0;    last =size-1;    while(first<=last)    {        position = first+ (last-first)*(key-array[first])/(array[last]-array[first]);        if(array[position] < key)            first=position+1;        else if(array[position] > key)            last=position-1;        else            return position;    }    return -1;}

 

4. Fibonacci search

Time Complexity: O (logn)
Advantage: average performance is better than half-lookup;
Disadvantage: it is required that the table to be searched be stored in sequence and must meet the following conditions: if the number of elements in an ordered table is n, and n is exactly (a Fibonacci number-1), the Fibonacci search method can be used.

 

Principle:

# Include <stdio. h> # include <stdlib. h> # include <string. h> int F [100]; int FibonacciSearch (int * a, int n, int key) {int low, high, mid, I, k = 0; low = 1; /* define the lowest subscript as the first record */high = n;/* define the highest subscript as the last record */while (n> F [k]-1) k ++; for (I = n; I <F [k]-1; I ++) a [I] = a [n]; while (low <= high) {mid = low + F [k-1]-1; if (key <a [mid]) {high = mid-1; k = K-1 ;} else if (key> a [mid]) {low = mid + 1; k = K-2;} else {if (mid <= n) return mid; /* if they are equal, the mid is the found position */else return n;} return-1;} int main () {int I; int arr [] = {, 16, 24, 35, 47, 59,62, 99}; F [0] = 0; F [1] = 1; for (I = 2; I <100; I ++) {F [I] = F [I-1] + F [I-2];} printf ("% d \ n", FIG (arr, sizeof (arr)/sizeof (int)-1, 99); return 0 ;}
 

 

5. Hash search

Time Complexity: O (1)

Applicable: data cannot be sorted or compared.

Principle:

A Method for directly Locating data elements by recording the relationship between data elements and storage addresses.

# Include "stdio. h "# include" stdlib. h "# include" io. h "# include" math. h "# include" time. h "# define OK 1 # define ERROR 0 # define TRUE 1 # define FALSE 0 # define MAXSIZE 100/* initial storage space allocation */# define SUCCESS 1 # define UNSUCCESS 0 # define HASHSIZE 12/* defines the length of the hash table as an array */# define NULLKEY-32768 typedef int Status; /* Status is the function type, and its value is the function result Status code, such as OK */typedef struct {int * elem;/* Data Element storage base address, dynamically allocate an array */int count;/* Current Data Number of elements */} HashTable; int m = 0;/* hash table length, global variable * // * initialize hash */Status InitHashTable (HashTable * H) {int I; m = HASHSIZE; H-> count = m; H-> elem = (int *) malloc (m * sizeof (int); for (I = 0; I <m; I ++) H-> elem [I] = NULLKEY; return OK;}/* Hash function */int Hash (int key) {return key % m;/* except the remaining keywords */}/* Insert the keyword into the hash list */void InsertHash (HashTable * H, int key) {int addr = Hash (key);/* calculate the Hash address */while (H-> elem [addr]! = NULLKEY)/* if not empty, the conflict */{addr = (addr + 1) % m; /* linear detection by open addressing Method */} H-> elem [addr] = key; /* Insert the keyword */}/* to search for the keyword */Status SearchHash (HashTable H, int key, int * addr) after there is a blank space) {* addr = Hash (key);/* calculate the Hash address */while (H. elem [* addr]! = Key)/* if not empty, the conflict */{* addr = (* addr + 1) % m; /* linear test of open addressing Method */if (H. elem [* addr] = NULLKEY | * addr = Hash (key)/* If the loop returns to the origin */return UNSUCCESS; /* indicates that the keyword does not exist */} return SUCCESS;} int main () {int arr [HASHSIZE] = }; int I, p, key, result; HashTable H; key = 39; InitHashTable (& H); for (I = 0; I <m; I ++) insertHash (& H, arr [I]); result = SearchHash (H, key, & p); if (result) printf ("Find % d address: % d \ n", key, p); else printf ("failed to find % d. \ N ", key); for (I = 0; I <m; I ++) {key = arr [I]; SearchHash (H, key, & p ); printf ("find % d address: % d \ n", key, p);} return 0 ;}

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.