forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.java
59 lines (43 loc) · 1.17 KB
/
bubblesort.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
class bubblesort
{ int A[];int n;
public bubblesort(int nn)
{ n=nn;
A=new int[n];
}// contructor to initialize data members
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements of the array");
for(int i=0;i<n;i++)
A[i]=sc.nextInt();
} // inputting size of the Array and the elements of the Array.
public void sort()
{
for(int i=0;i<n;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;
}
}
}
}//sorting the array using bubble sort technique
public void display()
{ sort();
for(int i=0;i<n;i++)
System.out.print(A[i]+" ");
}//Displaying the elements of the Array
public static void main(String args[])
{ System.out.println("Enter the size of the array");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
bubblesort ob=new bubblesort(n);
ob.input();
ob.display();
}//main function
}//class closed