22
SEARCH LINEAR BINARY

Search

Embed Size (px)

Citation preview

Page 1: Search

SEARCH

LINEAR BINARY

Page 2: 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 3: 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 4: Search

import java.io.*;class linearsearch {public static void main(String args[]) throws IOException {int i, sum = 0, ns, loc = 0;int[] mat = new int[101];DataInputStream in = new DataInputStream(System.in);System.out.print("Enter how many numbers in the array : ");int tn = Integer.parseInt(in.readLine()); for (i = 0; i < tn; i++){System.out.print("Enter the number " + (i+1) + " : ");mat[i] = Integer.parseInt(in.readLine());}System.out.print("Enter the element to search : ");ns = Integer.parseInt(in.readLine());for (i = 0; i < tn; i++){if (mat[i] == ns){loc = i+1;}}if (loc > 0)System.out.println("The number " + ns + " found at : " +loc);elseSystem.out.println("The number " + ns + " does not found");}}

Page 5: Search

Enter how many numbers in the array : 10

Enter the number 1 : 1Enter the number 2 : 3Enter the number 3 : 23Enter the number 4 : 44Enter the number 5 : 56Enter the number 6 : 65Enter the number 7 : 52Enter the number 8 : 76Enter the number 9 : 88Enter the number 10 : 34

Enter the element to search : 76

The number 76 found at : 8

Page 6: 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 7: 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 8: Search

P=LB+UB/2

24

STEP 1

FIND THE MIDDLE ELEMENT

P=0+9/2

Page 9: 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 10: 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 11: Search

3636 =

NUMBER FOUND

STEP 4

36 48

MIDDLE ELEMENT IS COMPARED WITH THE ELEMENTS IN THE ARRAY

Page 12: Search

import java.io.*;public class binary{public static void main(String a[])throws IOException{DataInputStream in=new DataInputStream(System.in);int i,ns,p,k;k=0;int m[]=new int[10];int lb=0;int ub=9;for(i=0;i<10;i++){System.out.println("enter the array");m[i]=Integer.parseInt(in.readLine());}System.out.println("enter the number to be searched");ns=Integer.parseInt(in.readLine());while(lb<=ub){p=(lb+ub)/2;if(m[p]<ns)lb=p+1;else if(m[p]>ns)ub=p-1;else if(m[p]==ns){k=1;break;}}if(k==1)System.out.println("found");elseSystem.out.println("notfound");}}

Page 13: 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 14: Search

SORTING

SELECTION BUBBLE

Page 15: Search

SELECTION SORT ARRAY ELEMENTS

4923 14 5 78 92 35 57

4923145 78 92 35 57

4923145 35 92 78 57

9223145 35 49 78 57

9223145 35 49 57 78

The sorted array

Page 16: Search

class selec { void x(int a[]) { int i,j,min,t; for(i=0;i<9;i++) { min=i; for(j=i+1;j<10;j++) { if(a[j]<a[min])min=j; } t=a[i]; a[i]=a[min]; a[min]=t; } System.out.println("Array in ascending order"); for(i=0;i<10;i++) System.out.println(a[i]); } }

Page 17: Search

23

14

5

35

49

57

BUBBLE SORT

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

Page 18: Search

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

Page 19: Search

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

Page 20: Search

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

Page 21: Search

23

14

5

35

49

57

23

14

5

35

49

57

Page 22: Search

class bub{ void x() { int i,j,t;int a[]={2,4,5,7,1,3,6,9,8,10};for(i=0;i<9;i++) {for(j=0;j<=9-1;j++){if(a[j]<a[j+1]){ t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }}for(i=0;i<10;i++) {System.out.println(a[i]); } }}