-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask4.java
More file actions
58 lines (51 loc) · 1.89 KB
/
task4.java
File metadata and controls
58 lines (51 loc) · 1.89 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
49
50
51
52
53
54
55
56
57
58
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.logging.*;
public class task4 {
public static void main(String[] args) throws IOException {
System.out.print("\033[H\033[J");
int[] array = createRandomArray(10, 0, 10);
System.out.print("Созданный массив: ");
System.out.println(showArray(array));
int[] sortArray = bubbleSort(array);
System.out.print("Отсортированный массив: ");
System.out.println(showArray(sortArray));
}
static int[] createRandomArray(int size, int downBound, int upperBond) {
Random rand = new Random();
int[] array = new int[size];
for(int i = 0; i < size; i++){
array[i] = rand.nextInt(upperBond) + downBound;
}
return array;
}
static String showArray(int[] array) {
return Arrays.toString(array);
}
static int[] bubbleSort(int[] array) throws IOException {
Logger log = logResult();
int[] sortArray = Arrays.copyOf(array, array.length);
int temp = 0;
for (int i = 0; i < sortArray.length; i++) {
for (int j = 0; j < sortArray.length - i - 1; j++) {
if (sortArray[j] > sortArray[j + 1]) {
temp = sortArray[j+1];
sortArray[j + 1] = sortArray[j];
sortArray[j] = temp;
log.log(Level.INFO, showArray(sortArray) + "\n");
}
}
}
return sortArray;
}
static Logger logResult() throws IOException {
Logger log = Logger.getLogger(task4.class.getName());
FileHandler fh = new FileHandler("log.txt", true);
log.addHandler(fh);
log.setUseParentHandlers(false);
SimpleFormatter sf = new SimpleFormatter();
fh.setFormatter(sf);
return log;
}
}