-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSolution.java
More file actions
48 lines (38 loc) · 1.33 KB
/
Solution.java
File metadata and controls
48 lines (38 loc) · 1.33 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
40
41
42
43
44
45
46
47
48
package Practice.CrackingTheCodingInterview.SortingBubbleSort;
import java.util.*;
public class Solution {
// Complete the countSwaps function below.
private static void countSwaps(int[] a) {
boolean sorted = false;
int numSwaps = 0;
while (!sorted) {
sorted = true;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i+1]) {
int tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
numSwaps++;
sorted = false;
}
}
}
System.out.println("Array is sorted in " + numSwaps + " swaps.");
System.out.println("First Element: " + a[0]);
System.out.println("Last Element: " + a[a.length - 1]);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] a = new int[n];
String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
}
countSwaps(a);
scanner.close();
}
}