Linear Search in C Language with Example Program

Spread the love
linear-search-in-c-language-with-example-programs

Program Description:

Write a Program to demonstrate the Linear Search in C programming language. The program should accept an array and number to be searched from the user, Then the program should check if the given number is found in the input array using the Linear Search Algorithm.

Example Input and Output:

Input:

Enter desired array size(1-100): 7

Please enter array elements(7) : 24 78 13 56 87 32 76

Enter a Number to search in Array: 13

Output:

FOUND - The 13 number Found at index 2

The target number 13 is found at the index 2 in the Input array 24 78 13 56 87 32 76.

Prerequisites:

It is recommended to know the basics of the Arrays and Functions in C language.

What is Linear Search:

Linear search is one of the most simple searching algorithms. The Linear search is used to search for an element in an array (Any Data structure).

In the Linear search, We check each element in the array one by one until the desired element (target element) is found or till the end of the array. If we reach the end of the array without finding the target element, Then the target element is not present in the given array.

The Time Complexity of the Linear Search is O(n) (Big O(n))

So It takes linear time to search for an element in the array. The Linear search is useful to search small arrays but can be inefficient if the array size is very large (As we need to check all elements).

📢 Time Complexity of Linear Search is O(n)

One of the advantages of Linear Search is it can work even when the input array is Not Sorted ( Unlike Binary Search, Where the array must be Sorted).

Linear Search in C Algorithm:

Here is the Algorithm for the linear Search in programming.

Let’s say the input array as the values and the size is the array size and the targetNum as the number to search.

  1. Iterate over the input array from the first element to the last element ( For Loop, Start from i=0 to i<size)
  2. At Each Iteration, Check if the current element ( values[i]) is equal to the target number( targetNum).
    • If the current element is equal to the target element, Then we found the element( targetNum), Return the Index of the current element.
    • If the current element is not equal to the target element, Then Continue to the next iteration.
  3. If the i reaches end of the array, Then targetNum is not found in the array, Return -1. (Not Found)

Therefore, we can determine whether the target number is present in the array by examining the returned Index.

Let’s look at the step-by-step explanation of the Linear Search Program in C language.

Linear Search in C Program Explanation:

  1. Start the program by declaring an array named values[100]. The values array can hold up to 100 elements. ( Change this number if you want to try with large arrays)
  2. We defined two functions. readInput() function and the linear_search() function. Let’s look at them in detail.
  3. The readInput() function is used to take the input values for the array elements. So it takes two formal arguments the values array and its size. The readInput() function updates the values array of elements based on user input.
  4. Call the readInput function from the main() function – readInput(values, size);
  5. Take the Number to be searched from the user i.e targetNum.
  6. Now, To search for the target number ( targetNum) in the values array, Call the linear_search() function with the values array, size and targetNum as the parameters. – linear_search(values, size, targetNum);
  7. The linear_search() function uses the linear search to search for the targetNum in the values array.
    • Uses a For loop to traverse through the array. Starts a For loop from i=0 and goes till the i<size, At Each Iteration,
      • Check if the values[i] is equal to targetNumif(values[i] == targetNum)
      • If the above condition is true, Then we found the targetNum in the values array. Return the index i back to the caller.
      • If the above condition is false, Then continue to the next element in the array. Increment the i by 1. and continue the above steps.
    • Once the for loop is completed and we still haven’t found the targetNum in the array, Then return -1.
  8. In the main() function, Grab the return value of the linear_search() function in index variable. – index = linear_search(values, size, targetNum);
  9. Check if the index == -1, If it is true, Then targetNum is not found in the values array. display the NOT FOUND message on the standard output.
  10. Otherwise, We found the targetNum in the values array, Display the FOUND message on the console.
  11. Stop the program.

Program to demonstrate Linear Search in C Language:

Here is the Linear Search Program in C language

Prototype details of the linear_search function

  • Function Name: linear_search
  • Argument List: (int values[], int size, int targetNum)
  • Return Type: int
    • returns index if the targetNum found in the values array
    • Otherwise, -1

Program Output:

Compile and Run the program.

We are using the GCC compiler on the Ubuntu Linux system.

$ gcc linear-search.c

Run the Program. The above command generates the executable file a.out

Let’s try a few more examples

As we can see from the above output, The target number 50 is found in the input array( 10 20 30 40 50) at index 4.

linear-search-in-c-program-output

In the above case, The target number 66 is not found in the input array( 99 88 77 33). So the program displayed the NOT FOUND - The 66 number Not Found in Array notice message.

Related Array Practice Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] have looked at the Linear Search in C program in earlier Posts, In today’s post, We will look at the Program to Count Frequency of Each Element […]

  2. […] Linear Search in C Language with Example Programs […]

Leave a Reply