-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubblesort.java
More file actions
30 lines (26 loc) · 839 Bytes
/
Copy pathBubblesort.java
File metadata and controls
30 lines (26 loc) · 839 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.Scanner;
public class Bubblesort {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n, temp;
System.out.println("enter number of elements in array");
n = s.nextInt();
int ar[] = new int[n];
System.out.println("enter elements");
for (int i = 0; i < n; i++) {
ar[i] = s.nextInt();
}
for (int i = 0; i < ar.length - 1; i++) {
for (int j = 0; j < ar.length - i - 1; j++) {
if (ar[j] > ar[j + 1]) {
temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
System.out.print(ar[i]);
}
}
}