-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
48 lines (37 loc) · 1.31 KB
/
Copy pathMain.java
File metadata and controls
48 lines (37 loc) · 1.31 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
import java.util.Arrays;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int[] unsortedArray = getRandomArray(5);
System.out.println(Arrays.toString(unsortedArray));
sortIntegers(unsortedArray);
}
public static int[] getRandomArray(int len) {
Random random = new Random();
int[] randomArray = new int[len];
for (int i = 0; i < len; i++) {
randomArray[i] = random.nextInt(1000);
}
return randomArray;
}
private static int[] sortIntegers(int[] array) {
System.out.println(Arrays.toString(array));
int[] sortedArray = Arrays.copyOf(array, array.length);
boolean flag = true;
int temp;
while (flag) {
flag = false;
for (int i = 0; i < sortedArray.length -1; i++) {
if (sortedArray[i] < sortedArray[i + 1]) {
temp = sortedArray[i];
sortedArray[i] = sortedArray[i + 1];
sortedArray[i + 1] = temp;
flag = true;
System.out.println("----->" + Arrays.toString(sortedArray));
}
}
System.out.println("-->" + Arrays.toString(sortedArray));
}
return sortedArray;
}
}