0
2.9kviews
Write a function to implement indexed Sequential Search, Explain with an Example
1 Answer
0
11views
  1. Consider the example as follows

enter image description here

  1. In the above diagram, if the user wants to search for "01235" data, then instead of searching in entire data store only subsection of datastore is searched.i.e Section A
  2. This subsection is selected based on the key value generated while searching.
  3. Following is the program for Indexed Sequential Search

    /*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.