-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java.txt
More file actions
40 lines (29 loc) · 1.3 KB
/
Copy pathSquare.java.txt
File metadata and controls
40 lines (29 loc) · 1.3 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
public class SquareNumber2 {
public static void main(String[] args) {
int[] numbers = {2, 1, 4, 3, 5, 9};
int[] sortedSquares = sortSquaredNumbers(numbers);
System.out.print("Squared numbers in ascending order: [");
for (int index = 0; index < sortedSquares.length; index++) {
System.out.printf("%d%s ", sortedSquares[index], (index < sortedSquares.length - 1) ? "," : "]");
}
}
public static int[] sortSquaredNumbers(int[] numbers) {
int[] squaredNumbers = new int[numbers.length];
for (int index = 0; index < numbers.length; index++) {
squaredNumbers[index] = numbers[index] * numbers[index];
}
boolean swapped;
for (int count = 0; count < squaredNumbers.length - 1; count++) {
swapped = false;
for (int index = 0; index < squaredNumbers.length - count - 1; index++) {
if (squaredNumbers[index] > squaredNumbers[index + 1]) {
int compare = squaredNumbers[index];
squaredNumbers[index] = squaredNumbers[index + 1];
squaredNumbers[index + 1] = compare;
swapped = true;
}
}
if (!swapped) break;
}
return squaredNumbers;
}