-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay20.java
51 lines (42 loc) · 1.48 KB
/
Day20.java
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
49
50
51
import java.util.Scanner;
public class Day20 {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int length = SCANNER.nextInt();
int[] array = getArray(length);
int swaps = bubbleSort(array);
System.out.println("Array is sorted in " + swaps + " swaps.");
System.out.println("First Element: " + array[0]);
System.out.println("Last Element: " + array[array.length - 1]);
}
private static int bubbleSort(int[] array) {
int totalSwaps = 0;
for (int i = 0; i < array.length; i++) {
int numberOfSwaps = 0;
for (int j = 0; j < array.length - 1 ; j++) {
// Swap adjacent elements if they are in decreasing order
if (array[j] > array[j + 1]) {
swap(array, j, j + 1);
numberOfSwaps++;
}
}
totalSwaps += numberOfSwaps;
if (numberOfSwaps == 0) {
break;
}
}
return totalSwaps;
}
private static void swap(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
private static int[] getArray(int length) {
int[] array = new int[length];
for (int index = 0 ; index < length ; index++) {
array[index] = SCANNER.nextInt();
}
return array;
}
}