-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.java
More file actions
27 lines (26 loc) · 862 Bytes
/
bubbleSort.java
File metadata and controls
27 lines (26 loc) · 862 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
import java.util.Scanner;
//bubbleSort in Java;
public class bubbleSort {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int n = sc.nextInt();
int[] arr = new int[n];
int i, j, temp;
for (i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (i = 0; i < arr.length - 1; i++) {
for (j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
}