forked from Ayush7-BYTE/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
29 lines (27 loc) · 824 Bytes
/
BubbleSort.java
File metadata and controls
29 lines (27 loc) · 824 Bytes
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
import java.util.*;
public class BubbleSort {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
System.out.println("Enter the length of the array to be sorted ");
int n=sc.nextInt();
System.out.println("Enter teh array elements ");
int[] a=new int[n];
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-i-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print("Sorted array in Ascending order is as follows: ");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}