-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
112 lines (81 loc) · 3.43 KB
/
Main.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package sorting_algorithm.part1;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] unsortedArray = {1, 2, 3, 4, 8, 6, 7, 5};
System.out.println(Arrays.toString(unsortedArray));
choiceSort(unsortedArray);
bubbleSort(unsortedArray);
insertSort(unsortedArray);
}
public static void choiceSort(int[] arrayToSort) {
int[] array = Arrays.copyOf(arrayToSort, arrayToSort.length);
System.out.println("Сортировка выбором");
//Проходим по массиву
//найти минимальный элемент
//меняем местами первый и минимальный элемент
//повторяем пока не пройдем по всему массиву
long start = System.nanoTime();
int indexMin;
for (int index = 0; index < array.length - 1; index++) {
indexMin = index;
for (int indexToScan = index; indexToScan < array.length; indexToScan++) {
if (array[indexMin] > array[indexToScan]) {
indexMin = indexToScan;
}
}
int temp = array[index];
array[index] = array[indexMin];
array[indexMin] = temp;
}
long stop = System.nanoTime();
System.out.println(stop - start);
System.out.println(Arrays.toString(array));
}
public static void bubbleSort(int[] arrayToSort) {
int[] array = Arrays.copyOf(arrayToSort, arrayToSort.length);
System.out.println("Сортировка пузырьком");
//проходим по массиву
//сравниваем два соседних элемента
//повторить, пока не отсортируем
long start = System.nanoTime();
int changeCounter;
do {
changeCounter = 0;
for (int index = 0; index < array.length - 1; index++) {
if (array[index] > array[index + 1]) {
int temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
changeCounter++;
}
}
} while (changeCounter > 0);
long stop = System.nanoTime();
System.out.println(stop - start);
System.out.println(Arrays.toString(array));
}
public static void insertSort(int[] arrayToSort) {
int[] array = Arrays.copyOf(arrayToSort, arrayToSort.length);
System.out.println("Сортировка вставками");
//проходим по массиву
//выбираем элемент
//устанавливаем элемент на законное место
long start = System.nanoTime();
int element;
int indexToInsert;
for (int index = 0; index < array.length; index++) {
element = array[index];
indexToInsert = index;
while (indexToInsert > 0
&& array[indexToInsert-1] > element) {
array[indexToInsert] = array[indexToInsert - 1];
indexToInsert--;
array[indexToInsert] = element;
}
}
long stop = System.nanoTime();
System.out.println(stop - start);
System.out.println(Arrays.toString(array));
}
}