-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBubbleSorting.java
More file actions
76 lines (67 loc) · 2.43 KB
/
AddBubbleSorting.java
File metadata and controls
76 lines (67 loc) · 2.43 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.ArrayList;
import java.util.Scanner;
public class AddBubbleSortOfResults {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Передайте аргумент командного рядка <substring>");
return;
}
String substring = args[0];
Scanner scan = new Scanner(System.in);
ArrayList<String> lines = new ArrayList<>();
int maxlines = 100;
int linescounter = 0;
while (linescounter < maxlines && scan.hasNextLine()) {
String line = scan.nextLine();
lines.add(line);
linescounter++;
}
ArrayList<Result> results = new ArrayList<>();
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
int count = howManySubstrings(line, substring);
results.add(new Result(count, i));
}
bubbleSort(results);
for (Result result : results) {
System.out.println(result.getCount() + " " + result.getIndex());
}
}
// Підрахунок кількості входжень підрядка в рядок
private static int howManySubstrings(String line, String substring) {
int count = 0;
int index = line.indexOf(substring);
while (index != -1) {
count++;
index = line.indexOf(substring, index + 1);
}
return count;
}
// Зберігання кількості входжень та індексу рядка
static class Result {
private int count;
private int index;
public Result(int count, int index) {
this.count = count; // кількість входжень підрядка
this.index = index; // Індекс рядка
}
public int getCount() {
return count;
}
public int getIndex() {
return index;
}
}
private static void bubbleSort(ArrayList<Result> results) {
int n = results.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (results.get(j).getCount() > results.get(j + 1).getCount()) {
Result temp = results.get(j);
results.set(j, results.get(j + 1));
results.set(j + 1, temp);
}
}
}
}
}