_datastructure_c_impl:search#include<stdio.h> #include <stdlib.h> #define MaxSize 100#define indexsize 20typedef int keytype;//element definition typedef struct{keytype key;} datatype;//the type definition of the sequential table typedef struct{datatype LIST[MAXSIZE];INT length;} sstable;//the type definition of the Index Table typedef struct{keytype MAXKEY;INT Index;} Indextable[indexsize];//finds the element with the keyword x in the order table and returns the position of the element in the table if it is returned, otherwise 0int seqsearch (sstable s,datatype x) {int I=0;while (i <s.length&&s.list[i].key!=x.key)//starting from the first element of the Order table compare I++;if (S.list[i].key==x.key) return i+1;else return 0;} int SeqSearch2 (sstable s,datatype x) {int i=s.length; s.list[0].key=x.key;//surveillance Whistle S.list[0]while (s.list[i].key>x.key) I--;return i;} Binary finds the element with the keyword x in the ordered order table, returns 0int BinarySearch (sstable s,datatype x) {int low,high,mid;low=0;//If it finds a position that returns the element in the table) Sets the lower and upper bounds of the range of elements to be found High=s.length-1;while (Low<=high) {mid= (Low+high)/2;if (S.list[mid].key==x.key)//If an element is found, Returns the position of the element return Mid+1;else if (s.list[mid].key<x.key)//If the element indicated by mid is less than the keyword, modify the low pointer low=mid+1;else if (S.list[mid] . Key>x.key)//If Mid refersIf the element shown is greater than the keyword, modify the high pointer high=mid-1;} return 0;} Find the element with the keyword x in the main table s, and T is the Index table. Returns a 0int seqindexsearch (sstable s,indextable t,int m,datatype x) {//block lookup if the position of the returned element in the table is found; index lookup int i,j,bl;for (i=0;i< m;i++)//Use the Index table to determine the cell that contains the element if (T[i].maxkey>=x.key) break;if (i>m)//If the element you are looking for is not in the Index order table, return 0return 0;j=t[i].index ///The element to be looked up in the main table of the J unit if (i<m-1)//bl for the length of unit J Bl=t[i+1].index-t[i].index;elsebl=s.length-t[i].index;while (J<t[i ].INDEX+BL) if (s.list[j].key==x.key)//If the keyword is found, returns the location of the keyword in the main table return J+1;elsej++;return 0;} void Main () {sstable s1={{123,23,34,6,8,355,32,67},8}; Sstable s2={{11,23,32,35,39,41,45,67},8}; Sstable s3={{6,12,23,16,21,26,41,32,37,35,48,46,49,47,52,61,59,76,68,72},20};indextable T={{23,0},{41,5},{52,10} , {76,15}};D atatype x={32};int pos;if ((Pos=seqsearch (s1,x))!=0) printf ("Lookup of the Sequential table: the position of keyword 32 in the main table is:%2d\n", POS); elseprintf ("Find failed! \ n "), if ((Pos=binarysearch (s2,x))!=0) printf (" Binary lookup: keyword 32 position in the primary table is:%2d\n ", POS); elseprintf (" Lookup failed! \ n "), if ((Pos=seqindexsearch (s3,t,4,x))!=0) printf (" Lookup of Index order table: keyword 32 position in the main table:%2d\n ", POS); Elseprintf ("Find failed! \ n "); system (" Pause ");}
Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.
_datastructure_c_impl: Finding elements in a sequential table