-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble.java
More file actions
30 lines (28 loc) · 796 Bytes
/
Bubble.java
File metadata and controls
30 lines (28 loc) · 796 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
import java.util.*;
class Bubble{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int temp;
System.out.println("Enter number of elements: ");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter the array elements: ");
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
System.out.println("Bubble sort");
for(int i=0;i<n;i++){
for(int j=0;j<n-1-i;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("Sorted array is: ");
for(int i=0;i<n;i++){
System.out.println(a[i]);
}
}
}