How To Ideas | How To Articles | How To Tutorials


0

How to Perform Linear Search using C Language


Consider you have an array of 10 numbers and you want to find out position of a particular number. Linear Search can help you with that and it is one of the best criteria to search when you have un-ordered list of number.

Linear Search is a method for finding a particular in an array or list, which consists of checking every element of that array, one at a time and in sequence, until the desired value is found. In this tutorial, I will show you how to implement Linear Search using C language to search for a particular number in a list of n numbers (in my case n can have any positive value less than 9).

Algorithm:

  1. Let we have an array of 10 elements in variable array and every element can be accessed using array[0], array[1], . . ., array[9]. i.e. size of array is 10 and index is starting from 0 (not 1).
  2. Let we have to find value stored in variable num and temp is a temporary variable which will be used for iteration purpose.
  3. Initialize temp with 0.
  4. Check whether value of temp is less than size of array or not. If yes, continue to next step, otherwise go to step 7.
  5. Now check whether array[temp] is equal to num or not. If yes, go to step number 7, otherwise continue to next step.
  6. Increment value of variable temp by 1 and move to 4th step.
  7. Check if value of temporary variable is equal to size of array. If yes, then no match is found and move to step 9, otherwise move to next step.
  8. Match is found at index temp or at position temp + 1. i.e. array[temp + 1] == num.
  9. Exit the program.

Instructions:

  1. For this program, we need only a single header file which is mentioned below.
    #include <stdio.h>
  2. In function main declare an array of 10 numbers, a temporary variable temp for iteration purpose, num variable for storing the value to be found and size variable which will be used to store size of the array. Now ask user to enter size of the array and then ask him/her to enter all the number for the array.
    int array[10], size, num, temp;
    printf("Enter size of array : ");
    scanf("%d", &size);
    for(temp = 0; temp < size; temp++)
    {
        printf("Enter Element #%d : ", temp + 1);
        scanf("%d", &array[temp]);
    }
  3. Now ask the user to enter the number he/she wants to search in the list.
    printf("\n Enter the number you want to search : ");
    scanf("%d", &num);
  4. Now using a for loop (which will run at max equal to size input by user) check whether array contains an element that user wants to search or not using algorithm explained earlier. If any time, it founds a match, the loop will break.
    for(temp = 0; temp < size; temp++)
    {
        if(array[temp] == num)
            break;
    }
  5. Now check whether value of temp is equal to size of array or not. If yes, then it means for loop finished completely and break command is not triggered i.e no match is found. But if it is less than the size of array, then we indeed found a match at position temp + 1 (because temp starts at 0).
    if(temp == size)
        printf("Number is not present in the array...");
    else
        printf("Number found at #%d", temp + 1);

Here are two screenshots of the working sample code.

Linear Search Using C (Match Found)

Linear Search Using C (Match Found)

Linear Search Using C (No Match Found)

Linear Search Using C (No Match Found)

You can also download sample code from here.

Filed in: C Tags: , , , , ,

Leave a Reply

Submit Comment



© 6757 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.