Вы находитесь на странице: 1из 3

Index sequential search:

Indexing mechanism used to speed up access to desired data. e.g author catalog in library.

Search key is nothing but an attribute or a set of attributes used to look up records.

Other than the main file where records are stored, index file is created.
An index file consists of records of the form

Search_Key Pointer (to record in main file)

To find a specific record for the given key value, index file is searched for the given key value.

/* Search an element in float array of size 20 using index sequential search.


i.e. create index array to search element*/
For e.g
Index Array elements
Key Ptr Array a
0 10.1 0 a[0] 10.1
1 35.6 5 a[1] 12.0
2 70.8 10 a[2] 15.2
3 86.1 15 a[3] 24.3
a[4] 30.1
a[5] 35.6
a[6] 39.7
a[7] 42.8
a[8] 47.5
a[9] 67.7
a[10 70.8
]
a[11] 74.5
a[12 78.4
]
a[13 80.3
]
a[14 83.2
]
a[15 86.1
]
a[16 90.5
]
a[17 91.7
]
a[18 93.7
]
a[19 99.8
]

#include<stdio.h>
#include<stdlib.h>
typedef struct
{
float key; //key
int ptr; //pointer position of key
}Index;
int main()
{
float a[20]; //sorted array of elements
Index in[5]; //index array
float ele; //data to be searched

printf(Enter the number of elements:);


scanf(%d,&n);
for(i=0;i<20;i++) //enter data in sorted order
scanf(%f,&a[i]);

for(i=0,j=0;i<n;i+=5,j++) //index array is populated


{
in.key[j] = a[i]; //a[0],a[5],a[10],a[15]
in.ptr[j]= i; //0,5,10,15
}
printf(Enter the number to be searched:);
scanf(%f,&ele);
if(ele < in.key[0])
{
printf(element not found);
exit(0);
}
for(i =1 ; i < j ;i++)
{
if( ele < in.key[i]) //compare element with index key
{
start = in.ptr[i-1]; //if smaller then element is found in range ptr[i-1] to ptr[i]
end = in.ptr[i];
break;
}
}
if ( i == j) //if ele >= in.key[i-1]
{
start = in.ptr[i-1];
end = n-1;
}
for(i=start;i<=end;i++)
{
if(ele==a[i])
{
flag=1;
break;
}
}
if(flag==1)
printf(element found at position %d,i);
else
printf(element not found);

return 0;
}

Вам также может понравиться