Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit59290a3

Browse files
authored
Update arx.java
1 parent1cb76ac commit59290a3

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
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+
}
1211
}
1312
System.out.println("Element not found in the array");
1413
}
1514

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+
}
2242
}
2343
}
24-
classMain{
44+
45+
classMain {
2546
publicstaticvoidmain(String[]args) {
26-
Scannersc=newScanner(System.in);
27-
intn;
47+
Scannersc =newScanner(System.in);
48+
intn,choice;
2849
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");
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=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();
4664
}
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){
5169
case1:
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-
intkey=sc.nextInt();
55-
se.linearSearch(arr,key);
72+
intkey =sc.nextInt();
73+
search.linearSearch(arr,key);
74+
break;
5675

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp