Data Structure Tutorial

Programming Approach

Flow Chart

Define Data Structure

DS Problem Solving

Algorithm

DS Data Types

DS Arrays

DS Stack

DS Queue

Number System

Spare matrix

Structure

Polynomials

Application of Stack

DS Linked List

DS Functions

DS Tree

DS Graph

Searching

Exercise Searching

Sorting

Hash Table

DS Questions

Free Workshop on Course Awareness Program for BCA, MCA, IT Students, By ANKIT SIR on Date: 4thFebruary 2023, Venue: Rajouri Garden, near Ramesh Nagar Metro Station. (Topic Highlight - Computer Programming, Website Development). For Registration or any query Whatsapp no. 8287212590

Searching techniques

The search techniques allow you to locate an element of your choice from the list. The element to be search is compared with all the elements of the list and if a perfect match is found, then the element is retrieved and a message is displayed. Searching is a process of finding an element within the list of elements stored in any order or randomly.

Types of searching:

  1. Linear Searching
  2. Sequential Searching
  3. Binary Searching

1. Linear Searching: The linear search is a straight forward method of retrieving elements. This is often considered the simplest technique for searching an unordered list of elements. In Linear search we can access each element one by one till end. When an appropriate match is found, a message is displayed.

#include<stdio.h>
#include<conio.h>
void main()
{
   int num[10], i, pos=-1, value;
   clrscr();
   printf("Enter 10 numbers:");
   for(i=0;i<10;i++)
   {
      scanf("%d",&num[i]);
   }
   printf("Enter number to be search:");
   scanf("%d",&value);
   for(i=0;i<10;i++)
   {
      if(value==num[i])
      {
         pos=i+1;
         break;
      }
   }
   if(pos==-1)
   {
      printf("\nThe element %d not found",value);
   }
   else
   {
      printf("\nThe position of %d is %d", value, pos);
   }
   getch();
}
Enter 10 numbers: 10 20 30 40 50 60 70 80 90 100
Enter number to be search: 50
The position of 50 is 5

2. Sequential searching: In Sequential searching the list has to be arranged in a particular ascending order. The list is compared only till it is found or the is smaller than the number to be compared.

3. Binary Searching: Another simple method of accessing a list of elements is the binary search. Binary search is possible only if the list of elements is ordered and in an ascending manner. To sort the list of elements, any of the sorting techniques can be used. Once the list is ready, you can begin the binary search operation. Complexity of Binary Search is O (log2(n)).
In this method, a lower bound (lb) and an upper bound (ub) are assigned to the 1st and the last elements of the list. Once this is done, the "midpoint" of the list can be determined by finding the average of the lb and the ub. Once the midpoint is determined, the element to be searched is compared with the midpoint. If the element is matched, message is displayed and if the element is less than the midpoint, the rest of the search takes place with the elements on the left side of the midpoint. If the element is greater than the midpoint, then the comparison takes place with the elements on the right side of the midpoint.

#include<stdio.h>
#include<conio.h>
void main()
{
   int num[10], i, beg, mid, end, pos=-1, value;
   clrscr();
   printf("Enter 10 numbers:");
   for(i=0;i<10;i++)
   {
      scanf("%d",&num[i]);
   }
   printf("Enter number to be search:");
   scanf("%d",&value);
   beg = 0;
   end = 10-1;
   while(beg<=end)
   {
      mid=(beg+end)/2;
      if(value==num[mid])
      {
         pos=mid+1;
         break;
      }
      else if(value >=num[mid])
      {
         beg=mid+1;
      }
      else
      {
         end=mid-1;
      }
   }
   if(pos==-1)
   {
      printf("\nThe element %d not found",value);
   }
   else
   {
      printf("\nThe position of %d is %d", value, pos);
   }
   getch();
}
Enter 10 numbers: 10 20 30 40 50 60 70 80 90 100
Enter number to be search: 50
The position of 50 is 5
Updated: 29-Jul-19