|
1 | 1 | importjava.util.Scanner; |
2 | | -classSearch{ |
3 | | -publicvoidlinearsearch(arr,key) |
4 | | - { |
5 | | -for(inti=0;i<arr.length;i++) |
6 | | - { |
7 | | -if(arr[i]==key) |
8 | | - { |
9 | | -System.out.println("Element found at Index"+i); |
10 | | -return; |
11 | | - } |
| 2 | + |
| 3 | +classSearch { |
| 4 | +// Linear search works on a sorted array or an unsorted array |
| 5 | +publicvoidlinearSearch(int[]arr,intkey) { |
| 6 | +for (inti =0;i <arr.length;i++) { |
| 7 | +if (arr[i] ==key) { |
| 8 | +System.out.println("Element found at Index " +i); |
| 9 | +return; |
| 10 | + } |
12 | 11 | } |
13 | 12 | System.out.println("Element not found in the array"); |
14 | 13 | } |
15 | 14 |
|
16 | | -publicvoidBinarysearch(arr,element) |
17 | | - { |
18 | | -while(l<=r) |
19 | | - { |
20 | | - |
21 | | - } |
| 15 | +// To perform binary search, the array should be sorted |
| 16 | +publicvoidbinarySearch(int[]arr,intelement) { |
| 17 | +intl =0,r =arr.length -1,mid; |
| 18 | +while (l <=r) { |
| 19 | +mid =l + (r -l) /2; |
| 20 | +if (arr[mid] ==element) { |
| 21 | +System.out.println("Element found at index: " +mid); |
| 22 | +return; |
| 23 | + }elseif (arr[mid] <element) { |
| 24 | +l =mid +1; |
| 25 | + }else { |
| 26 | +r =mid -1; |
| 27 | + } |
| 28 | + } |
| 29 | +System.out.println("Element not found"); |
| 30 | + } |
| 31 | + |
| 32 | +publicvoidbubbleSort(int[]arr) { |
| 33 | +for (inti =0;i <arr.length;i++) { |
| 34 | +for (intj =0;j <arr.length -1 -i;j++) { |
| 35 | +if (arr[j] >arr[j +1]) { |
| 36 | +inttemp =arr[j]; |
| 37 | +arr[j] =arr[j +1]; |
| 38 | +arr[j +1] =temp; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
22 | 42 | } |
23 | 43 | } |
24 | | -classMain{ |
| 44 | + |
| 45 | +classMain { |
25 | 46 | publicstaticvoidmain(String[]args) { |
26 | | -Scannersc=newScanner(System.in); |
27 | | -intn; |
| 47 | +Scannersc =newScanner(System.in); |
| 48 | +intn,choice; |
28 | 49 | int[]arr; |
29 | | -System.out.println("MENU DRIVEN ARRAY SORT- SEARCH PROGRAM"); |
| 50 | +Searchsearch =newSearch(); |
| 51 | + |
| 52 | +System.out.println("MENU DRIVEN ARRAY SORT-SEARCH PROGRAM"); |
30 | 53 | System.out.println("ALGORITHM - Time Complexity"); |
31 | | -System.out.println("1.) Linear Search - O(n)"); |
32 | | -System.out.println("2.) Binary Search - O(logn)"); |
33 | | -System.out.println("3.) Bubble sort - O(n^2)"); |
34 | | -System.out.println("4.) Selection sort - O(n^2)"); |
35 | | -System.out.println("5.) Insertion Sort - O(n^2)"); |
36 | | -System.out.println("6.) Merge Sort - O(n logn)"); |
37 | | -System.out.println("7.) Radix sort - O(nk)"); |
38 | | -do{ |
| 54 | +System.out.println("1.) Linear Search - O(n)"); |
| 55 | +System.out.println("2.) Binary Search - O(logn)"); |
| 56 | +System.out.println("3.) Bubble sort - O(n^2)"); |
| 57 | + |
39 | 58 | System.out.println("Enter the size of the array:"); |
40 | | -n=sc.nextInt(); |
41 | | -arr=newint[n]; |
42 | | -System.out.println("Enter"+n+"elements in an array:"); |
43 | | -for(inti=1;i,n;i++) |
44 | | - { |
45 | | -arr[i]=sc.nextInt(); |
| 59 | +n =sc.nextInt(); |
| 60 | +arr =newint[n]; |
| 61 | +System.out.println("Enter " +n +" elements in the array:"); |
| 62 | +for (inti =0;i <n;i++) { |
| 63 | +arr[i] =sc.nextInt(); |
46 | 64 | } |
47 | | -intchoice=sc.nextInt(); |
48 | | -Searchse =newSearch(); |
49 | | -switch(choice) |
50 | | - { |
| 65 | + |
| 66 | +System.out.println("Enter your choice:"); |
| 67 | +choice =sc.nextInt(); |
| 68 | +switch (choice){ |
51 | 69 | case1: |
52 | | -System.out.println("Searching Algorithm : Linear Search") |
| 70 | +System.out.println("Searching Algorithm : Linear Search"); |
53 | 71 | System.out.println("Enter the Key to search:"); |
54 | | -intkey=sc.nextInt(); |
55 | | -se.linearSearch(arr,key); |
| 72 | +intkey =sc.nextInt(); |
| 73 | +search.linearSearch(arr,key); |
| 74 | +break; |
56 | 75 |
|
57 | | -case2: |
58 | | -System.out.println("Searching Algorithm : Binary Search") |
59 | | - |
60 | | - |
61 | | - } |
62 | | - |
63 | | - } |
| 76 | +case2: |
| 77 | +System.out.println("Searching Algorithm : Binary Search"); |
| 78 | +System.out.println("Enter the Element to search:"); |
| 79 | +intelement =sc.nextInt(); |
| 80 | +search.binarySearch(arr,element); |
| 81 | +break; |
| 82 | + |
| 83 | +case3: |
| 84 | +System.out.println("Sorting Algorithm : Bubble Sort"); |
| 85 | +search.bubbleSort(arr); |
| 86 | +System.out.println("Array after Bubble Sort:"); |
| 87 | +for (inti =0;i <arr.length;i++) { |
| 88 | +System.out.print(arr[i] +" "); |
| 89 | + } |
| 90 | +break; |
64 | 91 |
|
65 | | - |
66 | | - |
67 | | - |
| 92 | +default: |
| 93 | +System.out.println("Invalid Choice"); |
| 94 | +} |
68 | 95 | } |
69 | 96 | } |