Search

Preview:

Citation preview

SEARCH

LINEAR BINARY

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]

LINEAR SEARCH

10 46 8 996 2 34 19578

2 2 2 2 2 2

THE NUMBER IS FOUND IN THE 6TH POSITION

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

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

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]

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

P=LB+UB/2

24

STEP 1

FIND THE MIDDLE ELEMENT

P=0+9/2

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

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

3636 =

NUMBER FOUND

STEP 4

36 48

MIDDLE ELEMENT IS COMPARED WITH THE ELEMENTS IN THE ARRAY

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

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

SORTING

SELECTION BUBBLE

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

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

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

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

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

23

14

5

35

49

57

23

14

5

35

49

57

23

14

5

35

49

57

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

Recommended