Improvement of binary search-difference value search and binary improvement-difference value
Difference value search
In binary search, we can exclude half of the data volume for each comparison, which is highly efficient. If you use the keyword information, the amount of data excluded each time depends on the size of the keyword, the search will be more efficient. This is the idea of difference search.
The following uses the sample code to compare the differences between the binary search and the difference search, so that we can see the improvements of the difference search in different ways.
# Include <stdio. h> # include <stdlib. h> int InterSearch (int * array, int n, int key) {if (array & n> 0) {int low, high, mid; low = 0, high = n-1; while (low <= high) {// mid = (low + high)/2; // binary lookup // difference lookup float rate = (float) (key-array [low])/(array [high]-array [low]); if (rate <0 | rate> 1) return-1; // there is a problem with the ratio value. The search failed: mid = low + (high-low) * rate; static int I = 1; printf ("find % d times \ n ", I ++); if (key < Array [mid]) high = mid-1; else if (key> array [mid]) low = mid + 1; elsereturn mid;} return-1 ;} return-1;} void main () {int array [] = {1, 3, 5, 6, 7, 8, 9, 11, 13 }; int key =-1; int sub = InterSearch (array, sizeof (array)/sizeof (int), key); if (sub! =-1) printf ("the subscript found is % d, key = % d \ n", sub, key); elseprintf ("failed to search! \ N "); system (" pause ");}
Run
Binary Search
Difference value search
Open comments to test the binary search and difference search respectively. You can also find a keyword that does not exist. The experiment found that the number of searches for difference values is significantly less than that for Binary searches.
Column directory
- Data structures and algorithms
- C pointer
Program for compiling sequential search algorithms
Search Algorithm set: sequential search, binary search, interpolation search, and dynamic search (Array Implementation and linked list implementation)
// Search. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include "LinkTable. h"
# Deprecision MAX_KEY 500
// ------------------------------ Array implementation section ----------------------------------
/**//*
Unordered array sequential search algorithm function nsq_Order_Search <implemented by array>
Parameter description:
Int array []: The queried array.
Int n: number of elements in the queried Array
Int key: the key value to be searched.
Return Value:
If not found: nsq_Order_Search =-1
Otherwise: nsq_Order_Search = key array subscript
*/
Int nsq_Order_Search (int array [], int n, int key)
...{
Int I;
Array [n] = key;
/** // * The semicolon after the for loop is required */
For (I = 0; key! = Array [I]; I ++ );
Return (I <n? I:-1 );
}
/**//*
Sequential array sequential search algorithm function sq_Order_Search <implemented by array>
Parameter description:
Int array []: The queried array.
Int n: number of elements in the queried Array
Int key: the key value to be searched.
Return Value:
If not found: sq_Order_Search =-1
Otherwise: sq_Order_Search = key array subscript
*/
Int sq_Order_Search (int array [], int n, int key)
...{
Int I;
Array [n] = MAX_KEY;
/** // * The semicolon after the for loop is required */
For (I = 0; key> array [I]; I ++ );
If (I <n & array [I] = key)
Return (I );
Else
Return (-1 );
}
/**//*
Ordered array Binary Search Algorithm function sq_Dichotomy_Search0 <implemented by array>
Parameter description:
Int array []: The queried array.
Int n: number of elements in the queried Array
Int key: the key value to be searched.
Return Value:
If not found: sq_Dichotomy_Sea ...... the remaining full text>
Binary Search must be faster than sequential search. Is this true? Why?
Error!
For example, there are 5 numbers, 1, 3, 4, 5.
To search for 1
Linear search only once
Binary Search (search for ordered Series)
First, 1 is compared with the intermediate number 3, and so on until the intermediate number is 1. Therefore, two times are required.
If it is helpful to you, please remember to adopt the best answer. Thank you!