21

Linear and Bianry search

Embed Size (px)

Citation preview

Page 1: Linear and Bianry search
Page 2: Linear and Bianry search

Mehedi Hassan 161-15-7667

Arafat Rahman 152-15-5983

Nazmul Islam 152-15-5652

Taijul Islam 152-15-5613

Page 3: Linear and Bianry search

Name :Ms. Rifat Ara ShamsDesignation :Senior LecturerDepartment :Department of Computer Science and EngineeringE-mail :[email protected] :+880-1723256560

Page 4: Linear and Bianry search
Page 5: Linear and Bianry search

LINEAR BINARY

SEARCH

Page 6: Linear and Bianry search

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]

Page 7: Linear and Bianry search

LINEAR SEARCH

10 46 8 996 2 34 19578

2 2 2 2 2 2

THE NUMBER IS FOUND IN THE 6TH POSITION

Page 8: Linear and Bianry search

#include <stdio.h> int main(){ int array[100], search, c, n;  printf("Enter the number of elements in array\n"); scanf("%d",&n);  printf("Enter %d integer(s)\n", n);for (c = 0; c < n; c++) scanf("%d", &array[c]);

 

CODE

Page 9: Linear and Bianry search

  printf("Enter the number to search\n"); 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;}

Page 10: Linear and Bianry search
Page 11: Linear and Bianry search

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]

Page 12: Linear and Bianry search

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

Page 13: Linear and Bianry search

P=LB+UB/2

24

STEP 1FIND THE MIDDLE ELEMENT

P=0+9/2

Page 14: Linear and Bianry search

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

Page 15: Linear and Bianry search

P=LB+UB/2 54

54 > 36

36 48

STEP 3DIVIDE THE ARRAY FURTHER

COMPARING SEARCH WITH MIDDLE ELEMENT

THE SEARCH ELEMENT LIES IN THE LB

Page 16: Linear and Bianry search

3636 =

NUMBER FOUND

STEP 4

36 48

MIDDLE ELEMENT IS COMPARED WITH THE ELEMENTS IN THE ARRAY

Page 17: Linear and Bianry search

#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

Page 18: Linear and Bianry search

    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;}

Page 19: Linear and Bianry search

Enter the elements of the array in ascending order12Enter the elements of the array in ascending order23Enter the elements of the array in ascending order34Enter the elements of the array in ascending order45Enter the elements of the array in ascending order56Enter the elements of the array in ascending order67Enter the elements of the array in ascending order78Enter the elements of the array in ascending order89Enter the elements of the array in ascending order98Enter the elements of the array in ascending order100Enter the number to be searched67found

Page 20: Linear and Bianry search
Page 21: Linear and Bianry search