Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 PRESENTATION ON SEARCHING www.ustudy.in

2 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

3 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

4 EXAMPLE [0][1][2][3][4][5][6][7][8][9] 102030405060708090100 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 7 th position www.ustudy.in

5 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

6 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

7 #include 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

8 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..,

9 if(lb>ub) { printf("element is not found"); return; } bin(a,lb,ub,item); } www.ustudy.in CONT..,

10 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

11 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

12 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

13 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

14 THANK YOU www.ustudy.in


Download ppt "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."

Similar presentations


Ads by Google