India's Largest Free Online Education Platform

C program for linear search

C program for linear search. Linear search in C programming: The following code implements linear search (Searching algorithm) which is used to find whether a given number is present in an array and if it is present then at what location it occurs. It is also known as sequential search.

C program for linear search

#include <stdio.h>
#include <stdlib.h>
int bubble_sort(int,int[]);    // function prototype
int main()
{ 
  int n;
  int arr[100];
  printf("\n This is a simple program for bubble sort\n");

  printf("\n Enter the size of array you want\n");
  scanf("%d",&n);     // taking input as length of array
  
  printf("\n Now Enter the elements of array\n");
  for(int i=0;i<n;i++)
  scanf("%d",&arr[i]);   // taking input as elements of array

  printf("Array elements entered by you is:\n ");
  for(int i=0;i<n;i++)
  printf("%d\t",arr[i]);  // printing entered array

  printf("\n\n Array elements after sorting :\n");
  bubble_sort(n,arr);  // function call
  printf("\n Sorted array elements after bubble sort");
  return 0;
}

int bubble_sort(int n,int arr[])   // function definition
{
  int temp;
  for (int j=0;j<n-1;j++)         // outer loop
    {
      for (int i=0;i<n-j-1;i++)   // inner loop
         {
           if(arr[i]>arr[i+1])
              {
               temp=arr[i+1];
               arr[i+1]=arr[i];
               arr[i]=temp;
              }
         }
    }

  for (int j=0;j<n;j++)   // displaying final array
  printf("%d \t",arr[j]);
  return 0;
}

Output:

C program for linear search
Output:1
C program for linear search
Output:2
C program for linear search2
Output:3

The above code can’t be directly copy-pasted. You must have to type with your own hand. This is made for your better future. 

Are you made for copy paste?

Absolutely “NO”

Then, try hard to understand this code and type with your own.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!