Skip to content

Commit 59290a3

Browse files
authored
Update arx.java
1 parent 1cb76ac commit 59290a3

File tree

1 file changed

+78
-51
lines changed

1 file changed

+78
-51
lines changed

arx.java

Lines changed: 78 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,96 @@
11
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+
}
1211
}
1312
System.out.println("Element not found in the array");
1413
}
1514

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+
}
2242
}
2343
}
24-
class Main{
44+
45+
class Main {
2546
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;
2849
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");
3053
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+
3958
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();
4664
}
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) {
5169
case 1:
52-
System.out.println("Searching Algorithm : Linear Search")
70+
System.out.println("Searching Algorithm : Linear Search");
5371
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;
5675

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;
6491

65-
66-
67-
92+
default:
93+
System.out.println("Invalid Choice");
94+
}
6895
}
6996
}

0 commit comments

Comments
 (0)