Ordered sequence retrieval algorithms (traversal search, binary search)

1. Problem

X ascending find elements in the array and returns its subscript; return 0 if there;

2. Parse

1, traverse to find
2, binary search

3. Design

1, through the array of all indices, each number and determines whether x is equal to his;
2, with the function recursively, each half the length of the current, is determined by the intermediate values x in the array on the left or the right array; Analyzing the array incoming again until the final length of the array is one, if x is a subscript is returned, if not for the x, 0 is returned.

4. Analysis

1、 Int date=-1;
for(int i=0;i<n;i++){
if(T[i]x){
date=i;
break;
}
If(date
-1)j=0;
else j=date;
2、 int divide(int l,int r){
if(l==r)if(T[l]==x){
j=l;
return;
else {
int num=T[(l+r)/2];
if(num>=x)divide(l,(l+r)/2);
else divide((l+r)/2+1,r);
return;

5. Source

https://github.com/Bcxx/task_code/tree/master/find

Released six original articles · won praise 6 · views 88

Guess you like

Origin blog.csdn.net/qq_45525352/article/details/104934647
Recommended