-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.java
More file actions
42 lines (34 loc) · 1003 Bytes
/
bubblesort.java
File metadata and controls
42 lines (34 loc) · 1003 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
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner;
public class bubblesort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s= new Scanner(System.in);
int i, j, k, swap,n;
System.out.println("Enter the number of elements");
n= s.nextInt();
int a[]= new int[n];
System.out.println("Enter the number to be sorted");
for(i=0;i<n;i++)
{
a[i]= s.nextInt();
}
//bubble sort starts
for(i=0;i<n-1;i++)
{
for(k=0;k<n-i-1;k++)
{
if(a[k] > a[k+1])
{
swap= a[k];
a[k]=a[k+1];
a[k+1]= swap;
}
}
}
System.out.println("Sorted List:");
for(i=0;i<n;i++)
{
System.out.println("Numbers are:" +a[i]);
}
}
}