-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting:BubbleSort.java
More file actions
39 lines (37 loc) · 1.12 KB
/
Copy pathSorting:BubbleSort.java
File metadata and controls
39 lines (37 loc) · 1.12 KB
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int bubbleSort(int a[]) {
int n = a.length;
boolean swapped = false;
int swappedCount = 0;
do {
swapped = false;
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
int tmp = a[i - 1];
a[i - 1] = a[i];
a[i] = tmp;
swapped = true;
swappedCount++;
}
}
} while (swapped);
return swappedCount;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
int swappedCount = bubbleSort(a);
System.out.println("Array is sorted in " + swappedCount + " swaps.");
System.out.println("First Element: " + a[0]);
System.out.println("Last Element: " + a[a.length - 1]);
}
}