forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-sort.java
40 lines (34 loc) · 941 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
import java.util.Scanner;
public class sorting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean sorted = true;
System.out.println("Enter the size of array: ");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the elements of array : ");
for (int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for (int i=0;i<n-1;i++)
{
for(int j = 0; j < n - 1 - i; j++)
{
if(a[j+1]<a[j])
{
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
sorted =false;
}
}
if(sorted)break;
}
System.out.println("Array after sorting :");
for (int item:a)
{
System.out.print(item+" ");
}
}
}