Successfully reported this slideshow.
Upcoming SlideShare
×

# Search

140 views

Published on

Search

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Search

1. 1. Mehedi Hassan 161-15-7667 Arafat Rahman 152-15-5983 Nazmul Islam 152- 15-5652 Taijul Islam 152- 15-5613
2. 2. Name : Ms. Rifat Ara Shams Designation : Senior Lecturer Department : Department of Computer Science and Engineering E-mail : rifat.cse@diu.edu.bd Cell-Phone : +880-1723256560
3. 3. LINEAR BINARY SEARCH
4. 4. LINEAR SEARCH 10 46 8 996 2 34 19578 ENTER THE ARRAY ELEMENTS ENTER THE NUMBER TO BE SEARCHED 2 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M{7] M[8] M[9]
5. 5. LINEAR SEARCH 10 46 8 996 2 34 19578 2 2 2 2 2 2 THE NUMBER IS FOUND IN THE 6TH POSITION
6. 6. #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter %d integer(s)n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); CODE
7. 7. printf("Enter the number to searchn"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) /* if required element found */ { printf("%d is present at location %d.n", search, c+1); break; } } if (c == n) printf("%d is not present in array.n", search); return 0; }
8. 8. BINARY SEARCH 1 5 8 2412 36 48 876754 ENTER THE ARRAY ELEMENTS ENTER THE NUMBER TO BE SEARCHED 36 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M{7] M[8] M[9]
9. 9. BINARY SEARCH 1 5 8 2412 36 48 876754 ENTER THE ARRAY ELEMENTS ENTER THE NUMBER TO BE SEARCHED 36 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M{7] M[8] M[9] LB UB
10. 10. P=LB+UB/2 24 STEP 1 FIND THE MIDDLE ELEMENT P=0+9/2
11. 11. 24 36< 36 48 54 67 87 THE SEARCH ELEMENT LIES IN THE UB 0 1 2 3 4 COMPARING SEARCH WITH MIDDLE ELEMENT STEP 2
12. 12. P=LB+UB/2 54 54 > 36 36 48 STEP 3 DIVIDE THE ARRAY FURTHER COMPARING SEARCH WITH MIDDLE ELEMENT THE SEARCH ELEMENT LIES IN THE LB
13. 13. 3636 = NUMBER FOUND STEP 4 36 48 MIDDLE ELEMENT IS COMPARED WITH THE ELEMENTS IN THE ARRAY
14. 14. #include<stdio.h> int main(){ int a[10],i,n,m,c=0,l,u,mid; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements in ascending order: "); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); CODE
15. 15. l=0,u=n-1; while(l<=u){ mid=(l+u)/2; if(m==a[mid]){ c=1; break; } else if(m<a[mid]){ u=mid-1; } else l=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found."); return 0; }
16. 16. Enter the elements of the array in ascending order 12 Enter the elements of the array in ascending order 23 Enter the elements of the array in ascending order 34 Enter the elements of the array in ascending order 45 Enter the elements of the array in ascending order 56 Enter the elements of the array in ascending order 67 Enter the elements of the array in ascending order 78 Enter the elements of the array in ascending order 89 Enter the elements of the array in ascending order 98 Enter the elements of the array in ascending order 100 Enter the number to be searched 67 found