presentation on searching . searching searching is the method to find element from the list of the...
TRANSCRIPT
PRESENTATION
ON
SEARCHING
www.ustudy.in
SEARCHING
• Searching is the method to find element from the list of
the elements. If element is found then it will display the
location of the elements else it will print element is not
found.
• Searching has two types. These are as follows:-
– Binary search
– Linear search
www.ustudy.in
BINARY SEARCH
• It works only on the sorted list.
• It finds the mid value with the help of formula that is :-
(lb+ub)\2 if mid value matched with the element which
we want to find then searching is completed else if
element is greater then mid then (lb=mid+1) if less then
(ub=mid-1) then it take another mid to find the element
this process is continued till then element is not found.
www.ustudy.in
EXAMPLE
[0][1] [2] [3] [4] [5] [6] [7] [8] [9]
10 20 30 40 50 60 70 80 90 100
Suppose we want to find 80 then we have to take mid and mid =4
80 is not matched with the mid take another mid that is (5+9)/2=7
Then 80 is found on 7th position
www.ustudy.in
ALGORITHM
• Binary(data,lb,ub,item,loc)
Here data is a sorted array with lower bound lb and uper bound ub, and item is
a given item of information. The variables beg, end and mid denote.
Respectiveily, the beginning, end and middle location of a segment of elements
of data this algorithm finds the location loc of item in data or sets loc=null
www.ustudy.in
ALGORITHM CONTD…
1. [INTIALIZE SEGMENT VERIABLES]
SET BEG:=LB,END:=UB AND MID:=INT((BEG+END)/2).
2. REPEAT STEP 3AND4 WHILE BEG<=END AND DATA[MID]!=ITEM.
3. IF ITEM<DATA[MID],THEN:
SET END:=MID-1
ELSE:
SET BEG:=MID+1
[END OF IF STRUCTURE]
4. SET MID:=INT((BEG+END)/2)
[END OF STEP 2LOOP]
5. IF DATA[MID]=ITEM,THEN:
SET LOC:=MID
ELSE:
SET LOC:=NULL
[END OF IF STRUCTURE]
6. EXIT. www.ustudy.in
• #include<stdio.h>• #include<conio.h>• void bin(int[],int,int,int);• int mid;• void main()• {• int a[5],i,item;• clrscr();• printf("enter any five no");• for(i=0;i<5;i++)• {• scanf("%d",&a[i]);• }• printf("enter item which u want to search");• scanf("%d",&item);• bin(a,0,4,item);• getch();• } www.ustudy.in
PROGRAM
• void bin(int a[],int lb,int ub,int item)• {• mid=(lb+ub)/2;• if(a[mid]==item)• {• printf("element is found at %d",mid+1 );• return;• }• else• {• if(a[mid]<item)• {• lb=mid+1;• }• else• {• ub=mid-1;• }
www.ustudy.in
CONT..,
• if(lb>ub)
• {
• printf("element is not found");
• return;
• }
• bin(a,lb,ub,item);
• }
• }
www.ustudy.in
CONT..,
LINEAR SEARCH
• linear search or sequential search is a method for finding a
particular value in a list, that consists of checking every one of its
elements, one at a time and in sequence, until the desired one is
found.
• Linear search is the simplest search algorithm; it is a special case of
brute-force search. Its worst case cost is proportional to the number
of elements in the list; and so is its expected cost, if all list elements
are equally likely to be searched for.
• Therefore, if the list has more than a few elements, other methods
(such as binary search or hashing) will be faster, but they also
impose additional requirements.
www.ustudy.in
ALGORITHM
• LINEAR_SEARCH(ARR, N, FIND)
• Where ARR is an array of N elements in which the element FIND is to be searched.
• STEP 1: Repeat For I=0,1,2,…..,N-1
• STEP 2: If (ARR[I]=FIND) Then
• RETURN I
• [End of If structure]
• STEP 3: [End of STEP 1 For loop]
• STEP 4: RETURN-1
• END LINEAR_SEARCH()
www.ustudy.in
PROGRAM• void main()• {• int a[10],i,n,m,c=0;• clrscr();• printf("Enter the size of an array");• scanf("%d",&n);• printf("\nEnter the elements of the array");• for(i=0;i<=n-1;i++){• scanf("%d",&a[i]);• }• printf("\nThe elements of an array are");• for(i=0;i<=n-1;i++)• {• printf(" %d",a[i]);• }• printf("\nEnter the number to be search");• scanf("%d",&m);
www.ustudy.in
CONT..,• for(i=0;i<=n-1;i++)• {• if(a[i]==m)• {• c=1;• break;• }• }• if(c==0)• printf("\nThe number is not in the list");• else• printf("\nThe number is found");• getch();• }
www.ustudy.in
THANK YOU
www.ustudy.in