Linear Search Program in Data Structure

Linear Search Program in Data Structure

At first, I want to share a story with you. A few days ago my friend Arif said to me “dude, can you bring the 30946 box for me from that room?”. Just he said, I went into the room.When I got into the room I was surprised!! I saw there were a huge number of boxes and the boxes were labeled by some specific codes. I was worried about to find out the 30946 box. I was thinking and thinking, thinking and thinking what should I do now!?. Suddenly I figured out an algorithm that I have learned in class. I started to visit each box from first to last and I was checking the box code to see whether it matches or not. After some time, I got the box and I stopped searching instantly. And I had brought the box to Arif. Arif was surprised!! He said, “How did you do that in a short time how!! how!!”. I said,”This is me you have to understand dude!! don’t underestimate the power of me”. He thanked me a lot of to help him and also he owes me a lunch treat. Though I got the whole credit in the case. But I know all credit goes to the algorithm. Please don’t say to Arif. Do you want to know the algorithm? cool cool, I will reveal everything just calm down. The algorithm was “Linear search” algorithm. do you want to know how does it work? Then let’s know how does it work.
 
              Linear Search is an algorithm that starts searching from first to the last step by step in a sequence and try to see whether it match or not with your given item. Assume that some data are stored in an array. Now you are given an item to the search from the array. What would you like to do? I think you would like to search from the beginning of the array and you will visit each element of the array and try to match with given item what I showed you above story. isn’t it? Same as Linear search algorithm does work. When you give an item to search, the Linear search algorithm picks the first element of the array and check the item to see whether it matched or not. If it doesn’t then it moves to the next element of the array and follow the same procedure. It will continue the same process until the item doesn’t match. When Linear search algorithm figures out the item from the array it stopped searching and display the result instantly. Exactly what I have done in the story and below picture.
 
 
Linear Search Program in Data Structure
Hopefully, now you have understood the whole process. I want to tell you the complexity of linear search algorithm is O(n). Now you can apply this and you can get a lunch treat from friends. So what are you waiting for? let’s do that.
 

Algorithm for Linear Search

Linear Search Program in Data Structure

Code:

#include<cstdio>
/** Here you can use #include<stdio.h> those are same
one use in cstdio use in .cpp file and stdio.h use in
.c file **/
 
#include<cstring>
/** same as string.h **/
 
void display(int location[], int location_size)
{
    if(location_size == 0)
        printf(“nItem not found!!n”);
    else
        printf(“nItem locations : “);
    for(int index = 0; index < location_size; index++)
    {
        printf(“%d”,location[index]);
 
        if(index < location_size – 2)
            printf(“, “);
        else if(index == location_size-2)
            printf(” and “);
        else
            printf(“n”);
    }
}
 
void linear_search(char Data_list[], int list_size, char item)
{
    int location[list_size];
    int location_index = 0;
    for(int index = 0; index < list_size; index++)
    {
        if(Data_list[index] == item)
            location[location_index++] = index+1;
    }
    display(location, location_index);
}
 
int main()
{
    char Data_list[] = {‘s’,’h’,’o’,’v’,’o’,’h’,’a’,’s’,’i’,’s’};
 
    printf(“Data list : “);
    for(int index = 0; index < 10; index++)
        printf(“%c “, Data_list[index]);
    printf(“n”);
 
    char item;
    printf(“nEnter your item : “);
    scanf(“%c”,&item);
 
    linear_search(Data_list,10,item);
    /** calling linear_search function for searching the given item **/
 
 
    return 0;
}
 
Be with us, stay happy. Please do your precious comment and give an opportunity to improve us. Happy coding.
 
 

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS

RELATED :

Related :

Related :

Leave a Comment