Home > Back-end >  Sequential search, binary search
Sequential search, binary search

Time:12-02

# include
# define list_size 20
Typedef int keytype;
Typedef int othertype;
Typedef struct
{
Othertype key;
Keytype other_data;
} recordtype;
Typedef struct
{
[list_size recordtype r + 1];
int length;
} recordlist;
//sequential search, a lookout
Int seqsearch (recordlist l, keytype k)
{
int i;
Their lives [0]. Key=k;
I=l.l ength;
While (their lives [I]. The key!=k)
i--;
return i;
}
//sequential search, not a lookout
Int SeqSearch (recordlist l, keytype k)
{
int i;
I=l.l ength;
While (i>=1 & amp; & Their lives [I]. The key!=k)
i--;
If (i>
=1)return i;
The else
return 0;
}
//binary search
Int rinsrch (recordlist l, keytype k)
{
Int mid;
Int low=0;
Int high=l.l ength;
While (low<=high)
{
Mid=+ high (low)/2;
If (k==their lives [mid] key)
return mid;
Else if (kHigh=mid - 1;
The else
Low=mid + 1;
}
return 0;
}
Int main ()
{
Recordlist l;
Int q, p, t, loc, len, I, length;
//create
Printf (" please enter the number of elements in the table: ");
The scanf (" % d ", & amp; Len);
Length=len;
Printf (" input sequence table elements: ");
for(i=0; i<=length; I++)//the equal sign
{
The scanf (" % d ", & amp; Their lives [I]);
}
//find
Printf (" please enter to find elements: ");

The scanf (" % d ", & amp; Loc);


Q=seqsearch (l, loc);
If (p!=0)
Printf (" element subscript 1: % d \ n ", q);
The else
Printf (" this element does not exist! \n");
P=SeqSearch (l, loc);
If (p!=0)
Printf (" element subscript 2: % d \ n ", q);
The else
Printf (" this element does not exist! \n");
T=rinsrch (l, loc);
If (t!=0)
Printf (" element subscript 3: % d \ n ", t);
The else
Printf (" this element does not exist! \n");

return 0;
}

Where is the wrong






CodePudding user response:

 # include 

# define list_size 20
Typedef int keytype;
Typedef int othertype;

Typedef struct
{
Othertype key;
Keytype other_data;
} recordtype;
Typedef struct
{
[list_size recordtype r + 1];
int length;
} recordlist;
//sequential search, a lookout
Int seqsearch (recordlist l, keytype k)
{
int i;
Their lives [0]. Key=k;
I=l.l ength;
While (I & gt; 0 & amp; & Their lives [I]. The key!=k)
i--;
return i;
}
//sequential search, not a lookout
Int SeqSearch (recordlist l, keytype k)
{
int i;
I=l.l ength;
While (i>=1 & amp; & Their lives [I]. The key!=k)
i--;
If (i>
=1)return i;
The else
return 0;
}
//binary search
Int rinsrch (recordlist l, keytype k)
{
Int mid;
Int low=0;
Int high=l.l ength;
While (low<=high)
{
Mid=+ high (low)/2;
If (k==their lives [mid] key)
return mid;
Else if (kHigh=mid - 1;
The else
Low=mid + 1;
}
return 0;
}
Int main ()
{
Recordlist l;
Int q, p, t, loc, len, I, length;
//create
Printf (" please enter the number of elements in the table: ");
The scanf (" % d ", & amp; Len);
If (len & lt; List_size)
Length=len;
The else
Length=list_size;
Printf (" input sequence table elements: ");
for(i=1; i<=length; I++)
{
The scanf (" % d ", & amp; Their lives [I] key);
}
L.l ength=length;
//find
Printf (" please enter to find elements: ");

The scanf (" % d ", & amp; Loc);


Q=seqsearch (l, loc);
//if (p!=0)
If (q!=0)
Printf (" element subscript 1: % d \ n ", q);
The else
Printf (" this element does not exist! \n");
P=SeqSearch (l, loc);
If (p!=0)
Printf (" element subscript 2: % d \ n ", q);
The else
Printf (" this element does not exist! \n");
T=rinsrch (l, loc);
If (t!=0)
Printf (" element subscript 3: % d \ n ", t);
The else
Printf (" this element does not exist! \n");

return 0;
}

For your reference ~

The wrong place, the length l no assignment, to whom the scanf assignment? An array access, etc.
Suggested to compare the code to find your own problem

CodePudding user response:

What is the meaning of l.l ength=length