-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble_Sort.java
45 lines (45 loc) · 895 Bytes
/
Bubble_Sort.java
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
import java.util.*;
public class Bubble_Sort
{
int a[];
int n;
Bubble_Sort(int n)
{
this.n=n;
a=new int[n];
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter array");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
}
void sort()
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void display()
{
System.out.println(Arrays.toString(a));
}
public static void main(String args[])
{
Bubble_Sort obj=new Bubble_Sort(5);
obj.input();
obj.display();
obj.sort();
obj.display();
}
}