forked from SoumyajitNag18/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble sorting and then Binary Searching with Array
61 lines (61 loc) · 1.5 KB
/
Bubble sorting and then Binary Searching with Array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;
class sort {
InputStreamReader read= new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter the array ");
n= Integer.parseInt(in.readLine());
for(i=0;i<n;i++)
{
System.out.println("Enter a number");
a[i]= Integer.parseInt(in.readLine());
}
}
void bubsort()
{
int j, temp;
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1)-i;j++){
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
System.out.println("Elements in sorted array : ");
for(i=0;i<n;i++)
{
System.out.print(ar[i]);
System.out.println();
}
}
void binsearch()throws IOException
{
int lb,ub,mid,found=0,item;
lb=0;
ub=n-1;
mid=0;
InputStreamReader read= new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter the number to be searched ");
item= Integer.parseInt(in.readLine());
while(found=0 && lb!=ub)
{
mid=(lb+ub)/2;
if(item<ar[mid])
ub=mid;
if(item>ar[mid])
lb=mid;
if(item==ar[mid])
found=1;
}
if(found == 1)
{
System.out.println("Search Successful!!");
System.out.println("Number found at position : "+mid);
}
else
System.out.println("Search not Successful!!");
}