Search

396 views
306 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
396
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Search

  1. 1. SEARCH LINEAR BINARY
  2. 2. 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]
  3. 3. LINEAR SEARCH 10 46 8 996 2 34 19578 2 2 2 2 2 2 THE NUMBER IS FOUND IN THE 6TH POSITION
  4. 4. 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); else System.out.println("The number " + ns + " does not found"); } }
  5. 5. Enter how many numbers in the array : 10 Enter the number 1 : 1 Enter the number 2 : 3 Enter the number 3 : 23 Enter the number 4 : 44 Enter the number 5 : 56 Enter the number 6 : 65 Enter the number 7 : 52 Enter the number 8 : 76 Enter the number 9 : 88 Enter the number 10 : 34 Enter the element to search : 76 The number 76 found at : 8
  6. 6. 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]
  7. 7. 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
  8. 8. P=LB+UB/2 24 STEP 1 FIND THE MIDDLE ELEMENT P=0+9/2
  9. 9. 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
  10. 10. 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
  11. 11. 3636 = NUMBER FOUND STEP 4 36 48 MIDDLE ELEMENT IS COMPARED WITH THE ELEMENTS IN THE ARRAY
  12. 12. 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"); else System.out.println("notfound"); } }
  13. 13. 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
  14. 14. SORTING SELECTION BUBBLE
  15. 15. 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
  16. 16. 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]); } }
  17. 17. 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
  18. 18. 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
  19. 19. 23 14 5 35 49 57 23 14 5 35 49 57 23 14 5 35 49 57 23 14 5 35 49 57
  20. 20. 23 14 5 35 49 57 23 14 5 35 49 57 23 14 5 35 49 57
  21. 21. 23 14 5 35 49 57 23 14 5 35 49 57
  22. 22. 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]); } } }

×