Tuesday 7 April 2020

LINEAR SEARCH

||  LINEAR SEARCH  ||


#1 WRITE A PROGRAM TO PERFORM LINEAR SEARCH IN C.

WHAT IS LINARY SEARCH?

Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.


LINEAR SEARCH EXAMPLE:



Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].
Examples :
Input : arr[] = {10, 20, 80, 30, 60, 50, 
                     110, 100, 130, 170}
          x = 110;
Output : 6
Element x is present at index 6

Input : arr[] = {10, 20, 80, 30, 60, 50, 
                     110, 100, 130, 170}
           x = 175;
Output : -1
Element x is not present in arr[].


Algorithm:


Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

CODE:

#include <stdio.h>
int main()
{
  int array[100], search, c, n;
  printf("Enter number of elements in array\n");
  scanf("%d", &n);
  printf("Enter %d integer(s)\n", n);
  for (= 0; c < n; c++)
    scanf("%d", &array[c]);
  printf("Enter a number to search\n");
  scanf("%d", &search);
  for (= 0; c < n; c++)
  {
    if (array[c] == search)
    {
      printf("%d is present at location %d.\n", search, c+1);
      break;
    }
  }
  if (== n)
    printf("%d isn't present in the array.\n", search);
  return 0;
}

YOUTUBE LINK:(FULL VIDEO ON LINEAR SEARCH) 🔻🔻🔻🔻🔻

THANKYOU:)
RAJ ARYAN


JAVA PROGRAMMING CODES || AryanTechsource

  PART-A 1. Implement a stack using a stack class which has methods push, pop. import java.util.Stack; class StackMain { public static void ...

Popular Posts