-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
100 lines (81 loc) · 2.61 KB
/
BubbleSort.java
File metadata and controls
100 lines (81 loc) · 2.61 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.Date;
class MyBubbleSort {
private int nElem;
private long[] a;
public MyBubbleSort(int size) {
a = new long[size];
nElem = 0;
}
public void display() {
for (int j = 0; j < nElem; j++)
System.out.println(a[j]);
System.out.println("");
}
public int size() {
return nElem;
}
public void insert(long value) { //вставка.
a[nElem] = value;
nElem++;
}
public void sort() { //сортировка
int in, out;
long temp;
for (out = nElem - 1; out > 1; out--)
for (in = 0; in < out; in++)
if (a[in] > a[in + 1]) {
swap(in, in+1);
}
}
public void swap(int one, int two) {
long temp = a[two];
a[two] = a[one];
a[one] = temp;
}
public void sort2() { //сортировка.
int in, out;
long temp;
long count=0;
for (out = nElem - 1; out > 1; out--) {
for (in = 0; in < out; in++)
if (a[in] > a[in + 1]) {
temp = a[in + 1];
a[in + 1] = a[in];
a[in] = temp;
count++;
}
}
System.out.println(count);
}
}
//////////////////////////////////////////////////////////////////
class MyBubbleSortApp{
static public void main(String[] args){
int maxSize = 4;
MyBubbleSort arr = new MyBubbleSort(maxSize);
arr.insert(35);
arr.insert(85);
arr.insert(1);
arr.insert(45);
// arr.insert(22);
arr.display();
arr.sort();
arr.display();
/*
for(int j=0; j<maxSize; j++) {//Заполнение массива случайными числами.
long n = (long) (java.lang.Math.random() * (maxSize - 1));
arr.insert(n);
}
*/
/*
for(int j=100000; j>0; j--) //вставка значений по убыванию.
arr.insert(j);
Date currentTime = new Date(); //получаем текущее время.
arr.sort2();//сортировка.
Date newTime = new Date(); //получаем новое текущее время.
long msDelay = newTime.getTime() - currentTime.getTime(); //вычисляем разницу
System.out.println("Длительность сортировки " + msDelay + " мс");
System.out.println("");
*/
}
}