C Language Search Algorithm order lookup, binary search (binary find), the recent test to use, the Internet also has a lot of examples, I think I wrote to understand some.
- Sequential Lookup
/*Sequential Lookup order lookups are the exact locations of the same number of given keywords in a known no (or ordered) Order queue. The principle is to have the keyword match the number in the queue from the last one (or the first one), until it finds the same number as the given keyword
Its disadvantage is inefficiency.*/#include<stdio.h>voidMain () {intI, num,arr[Ten]={3,6,9,Ten, $, at, the, $,2, One}; intSize =sizeof(arr)/sizeof(int); printf ("Please enter a value to query:"); scanf ("%d",&num); for(i=0; i<size;i++){ if(num==Arr[i]) { Break; } } if(i!=size) printf ("the value to query%d in the first%d locations", num,i+1); Elseprintf ("value%d not found", num); Getch ();}
- Two-point Search
/*Binary Lookup Two-point lookup is also called binary lookup (binary search), which is a more efficient method of finding. However, the binary lookup requires that the linear table must take a sequential storage structure, and that the elements in the table are ordered alphabetically by keyword. */#include<stdio.h>voidMain () {intmid,low,high,num,arr[Ten]={3,6,9,Ten, at, $, the, the, the,213}; intSize =sizeof(arr)/sizeof(int); printf ("Please enter a value to query:"); scanf ("%d",&num); Low=0; High= size;//values for initial low and high while(low<=High ) {Mid= (Low+high)/2;//Take medium value if(Arr[mid]==num) Break;//Find, end loop at this time Low<=high Else if(Arr[mid] < num) Low = mid+1;//if the target value is larger than the current middle value, the target value is moved to mid+1 after the median value ElseHigh = mid-1; } if(Low <= High)//Find, Outputprintf"the value to query%d in the first%d locations", num,mid+1); Else //not foundprintf"value%d not found", num); Getch ();}
C Language Search Algorithm order lookup, binary lookup (binary lookup)