|
1 | 1 | import java.util.Scanner;
|
2 |
| -class Search{ |
3 |
| - public void linearsearch(arr,key) |
4 |
| - { |
5 |
| - for(int i=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 | +class Search { |
| 4 | + // Linear search works on a sorted array or an unsorted array |
| 5 | + public void linearSearch(int[] arr, int key) { |
| 6 | + for (int i = 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 |
| - public void Binarysearch(arr,element) |
17 |
| - { |
18 |
| - while(l<=r) |
19 |
| - { |
20 |
| - |
21 |
| - } |
| 15 | + // To perform binary search, the array should be sorted |
| 16 | + public void binarySearch(int[] arr, int element) { |
| 17 | + int l = 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 | + } else if (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 | + public void bubbleSort(int[] arr) { |
| 33 | + for (int i = 0; i < arr.length; i++) { |
| 34 | + for (int j = 0; j < arr.length - 1 - i; j++) { |
| 35 | + if (arr[j] > arr[j + 1]) { |
| 36 | + int temp = arr[j]; |
| 37 | + arr[j] = arr[j + 1]; |
| 38 | + arr[j + 1] = temp; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
22 | 42 | }
|
23 | 43 | }
|
24 |
| -class Main{ |
| 44 | + |
| 45 | +class Main { |
25 | 46 | public static void main(String[] args) {
|
26 |
| - Scanner sc=new Scanner(System.in); |
27 |
| - int n; |
| 47 | + Scanner sc = new Scanner(System.in); |
| 48 | + int n, choice; |
28 | 49 | int[] arr;
|
29 |
| - System.out.println("MENU DRIVEN ARRAY SORT- SEARCH PROGRAM"); |
| 50 | + Search search = new Search(); |
| 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=new int[n]; |
42 |
| - System.out.println("Enter"+n+"elements in an array:"); |
43 |
| - for(int i=1;i,n;i++) |
44 |
| - { |
45 |
| - arr[i]=sc.nextInt(); |
| 59 | + n = sc.nextInt(); |
| 60 | + arr = new int[n]; |
| 61 | + System.out.println("Enter " + n + " elements in the array:"); |
| 62 | + for (int i = 0; i < n; i++) { |
| 63 | + arr[i] = sc.nextInt(); |
46 | 64 | }
|
47 |
| - int choice=sc.nextInt(); |
48 |
| - Search se = new Search(); |
49 |
| - switch(choice) |
50 |
| - { |
| 65 | + |
| 66 | + System.out.println("Enter your choice:"); |
| 67 | + choice = sc.nextInt(); |
| 68 | + switch (choice) { |
51 | 69 | case 1:
|
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 |
| - int key=sc.nextInt(); |
55 |
| - se.linearSearch(arr,key); |
| 72 | + int key = sc.nextInt(); |
| 73 | + search.linearSearch(arr, key); |
| 74 | + break; |
56 | 75 |
|
57 |
| - case 2: |
58 |
| - System.out.println("Searching Algorithm : Binary Search") |
59 |
| - |
60 |
| - |
61 |
| - } |
62 |
| - |
63 |
| - } |
| 76 | + case 2: |
| 77 | + System.out.println("Searching Algorithm : Binary Search"); |
| 78 | + System.out.println("Enter the Element to search:"); |
| 79 | + int element = sc.nextInt(); |
| 80 | + search.binarySearch(arr, element); |
| 81 | + break; |
| 82 | + |
| 83 | + case 3: |
| 84 | + System.out.println("Sorting Algorithm : Bubble Sort"); |
| 85 | + search.bubbleSort(arr); |
| 86 | + System.out.println("Array after Bubble Sort:"); |
| 87 | + for (int i = 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 | }
|
0 commit comments