Indexed Sequential Search

14 Mar, 2022
In this searching method, first of all, an index file is created, that contains some specific group or division of required record when the index is obtained, then the partial indexing takes less time cause it is located in a specified group.
Note: When the user makes a request for specific records it will find that index group first where that specific record is recorded.
In Indexed Sequential Search a sorted index is set aside in addition to the array.
Each element in the index points to a block of elements in the array or another expanded index.
The index is searched 1st then the array and guides the search in the array.
Note: Indexed Sequential Search actually does the indexing multiple time, like creating the index of an index.  
 
 
 
C
Java
Python3
C#
PHP
// C program for Indexed Sequential Search#include <stdio.h>#include <stdlib.h> void indexedSequentialSearch(int arr[], int n, int k){    int GN = 3; // GN is group number that is number of                // elements in a group    int elements[GN], indices[GN], i, set = 0;    int j = 0, ind = 0, start, end;    for (i = 0; i < n; i += 3) {         // Storing element        elements[ind] = arr[i];         // Storing the index        indices[ind] = i;        ind++;    }    if (k < elements[0]) {        printf("Not found");        exit(0);    }    else {        for (i = 1; i <= ind; i++)            if (k <= elements[i]) {                start = indices[i - 1];                end = indices[i];                set = 1;                break;            }    }    if (set == 0) {        start = indices[GN - 1];        end = GN;    }    for (i = start; i <= end; i++) {        if (k == arr[i]) {            j = 1;            break;        }    }    if (j == 1)        printf("Found at index %d", i);    else        printf("Not found");} // Driver codevoid main(){     int arr[] = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };    int n = sizeof(arr) / sizeof(arr[0]);     // Element to search    int k = 8;    indexedSequentialSearch(arr, n, k);}
// Java program for Indexed Sequential Search import java.io.*; class GFG {     static void indexedSequentialSearch(int arr[], int n,                                        int k)    {        int elements[] = new int[20];        int indices[] = new int[20];        int temp, i;        int j = 0, ind = 0, start = 0, end = 0, set = 0;        for (i = 0; i < n; i += 3) {             // Storing element            elements[ind] = arr[i];             // Storing the index            indices[ind] = i;            ind++;        }        if (k < elements[0]) {            System.out.println("Not found");            return;        }        else {            for (i = 1; i <= ind; i++)                if (k <= elements[i]) {                    start = indices[i - 1];                    set = 1;                    end = indices[i];                    break;                }        }        if (set == 0) {            start = indices[i - 1];            end = n;        }        for (i = start; i <= end; i++) {            if (k == arr[i]) {                j = 1;                break;            }        }        if (j == 1)            System.out.println("Found at index " + i);        else            System.out.println("Not found");    }     // Driver code     public static void main(String[] args)    {        int arr[] = { 6, 7, 8, 9, 10 };        int n = arr.length;         // Element to search        int k = 8;        indexedSequentialSearch(arr, n, k);    }}// This code is contributed by shs..
# Python program for Indexed# Sequential Search  def indexedSequentialSearch(arr, n, k):     elements = [0] * 20    indices = [0] * 20    j, ind, start, end = 0, 0, 0, 0    set_flag = 0         for i in range(0, n, 3):         # Storing element        elements[ind] = arr[i]         # Storing the index        indices[ind] = i        ind += 1     if k < elements[0]:        print("Not found")        exit(0)     else:         for i in range(1, ind + 1):            if k <= elements[i]:                start = indices[i - 1]                end = indices[i]                set_flag = 1                break    if set_flag == 0:        start = indices[i-1]        end = n    for i in range(start, end + 1):        if k == arr[i]:            j = 1            break     if j == 1:        print("Found at index", i)    else:        print("Not found")  # Driver codeif __name__ == "__main__":     arr = [6, 7, 8, 9, 10]    n = len(arr)     # Element to search    k = 8     # Function call    indexedSequentialSearch(arr, n, k) # This code is contributed by Ryuga
// C# program for Indexed Sequential Search using System; class GFG {       static void indexedSequentialSearch(int []arr, int n, int k){    int []elements = new int[20];    int []indices = new int[20];    int  i;    int j = 0, ind = 0, start=0, end=0, set = 0;    for (i = 0; i < n; i += 3) {         // Storing element        elements[ind] = arr[i];         // Storing the index        indices[ind] = i;        ind++;    }    if (k < elements[0]) {        Console.Write("Not found");        return;             }    else {        for (i = 1; i <= ind; i++)            if (k <= elements[i]) {                start = indices[i - 1];                  set = 1;                end = indices[i];                break;            }    }      if(set == 0)    {          start = indices[i-1];          end = n-1;    }    for (i = start; i <= end; i++) {        if (k == arr[i]) {            j = 1;            break;        }    }    if (j == 1)        Console.WriteLine("Found at index "+ i);    else        Console.WriteLine("Not found");} // Driver code     public static void Main () {        int []arr = { 6, 7, 8, 9, 10 };        int n = arr.Length;         // Element to search        int k = 10;        indexedSequentialSearch(arr, n, k);    }}// This code is contributed by shs..
<?php// PHP program for Indexed Sequential Search function indexedSequentialSearch($arr, $n, $k){    $elements = array();    $indices = array();    $temp = array();    $j = 0; $ind = 0; $start=0; $end=0; $set = 0;    for ($i = 0; $i < $n; $i += 3)    {         // Storing element        $elements[$ind] = $arr[$i];         // Storing the index        $indices[$ind] = $i;        $ind++;    }         if ($k < $elements[0])    {        echo "Not found";     }    else    {        for ($i = 1; $i <=$ind; $i++)            if ($k < $elements[$i])            {                $start = $indices[$i - 1];                 $set = 1;                $end = $indices[$i];                break;            }    }      if($set == 1)    {          $start = $indices[$i-1];          $end = $n;    }    for ($i = $start; $i <=$end; $i++)    {        if ($k == $arr[$i])        {            $j = 1;            break;        }    }    if ($j == 1)        echo "Found at index ", $i;    else        echo "Not found";} // Driver code$arr = array( 6, 7, 8, 9, 10 );$n = count($arr); // Element to search$k = 10;indexedSequentialSearch($arr, $n, $k); // This code is contributed by shs..?>
Found at index 2
 
Shashank12
ankthon
raunaksrivastava22
anjalijoshi710
Picked
Technical Scripter 2018
Searching
Technical Scripter
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Search, insert and delete in a sorted array
Given a sorted and rotated array, find if there is a pair with a given sum
Allocate minimum number of pages
Find common elements in three sorted arrays
3 Different ways to print Fibonacci series in Java
Find whether an array is subset of another array
Next Smaller Element
Find a pair with the given difference