0
2.0kviews
Write a program in C to search a list using indexed sequential search.What are the advantages of indexed sequential search over sequential search.
1 Answer
0
57views
  1. In Index sequential searching technique, first of all an index file is created that contains references to a group of records,.
  2. Once an index is obtained, the partial searching takes less time since it is to be located in the group/bucket specified by the index.
  3. The program given below creates an index file for the employee records by grouping the records and then locates the required key by searching the index first and then returns the required record.
  4. The advantage of indexed sequential over sequential is that here we can achieve both sequential as well as Random access (i.e. jump directly to a record from the index value).
  5. Moreover, it is faster than sequential method.
  6. The program to implement indexed sequential search is given below

    /*Indexed Sequential Search*/
    #include <stdio.h>
    #include <conio.h>
    #define MAX 5
    struct mainfile
    {
        int empid;
        char name[25];
        float basic;
    };
    struct indexfile
    {
        int index_id;
        int kindex;
    };
    void main()
    {
        struct mainfile fobj[MAX];
        struct indexfile index[MAX];
        int i, num, low, high, ct = 4;
        float basicsal;
        for (i = 0;i < MAX;i++)
        {
            printf("\nEnter employee id?");
            scanf("%d", &fobj[i].empid);
            printf("\nEnter name?");
            scanf("%s", fobj[i].name);
            printf("\nEnter basic?");
            scanf("%f", &basicsal);
            fobj[i].basic = basicsal;
        }
        printf("\nNow creating index file...!");
        for (i = 0;i < (MAX / 5);i++)
        {
            index[i].index_id = fobj[ct].empid;
            index[i].kindex = ct;
            ct = ct + 5;
        }
        printf("\n\nEnter the empid to search?");
        scanf("%d", &num);
        for (i = 0;(i < MAX / 5) && (index[i].index_id <= num);i++);
        low = index[i-1].kindex;
        high = index[i].kindex;
        for (i = low;i <= high;i++)
        {
            if (num == fobj[i].empid)
            {
                printf("\nThe record is: \n\t");
                printf("\nEmpid: %d", fobj[i].empid);
                printf("\nName: %s", fobj[i].name);
                printf("\nBasic: %f", fobj[i].basic);
                getch();
            }
        }
        printf("\nNumber not found...!");
    }
    
Please log in to add an answer.