-
Notifications
You must be signed in to change notification settings - Fork 88
/
mergesort.dart
62 lines (53 loc) · 1.31 KB
/
mergesort.dart
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
void main(){
List <Comparable> a = [5,9,1,3,4,6,6,3,2];
sort(a);
print(a);
}
void sort(List<Comparable> a){
List<Comparable> aux = new List<Comparable>(a.length);
sortHighLow(a, aux, 0, a.length-1);
assert(_isSorted(a));
}
void sortHighLow(List<Comparable> a, List<Comparable> aux, int lo, int hi){
if (hi <= lo){
return;
}
int mid = lo + ((hi - lo) ~/ 2);
sortHighLow(a, aux, lo, mid);
sortHighLow(a, aux, mid + 1, hi);
merge(a, aux, lo, mid, hi);
}
void merge(List<Comparable> a, List<Comparable> aux, int lo, int mid, int hi) {
assert(_isSortedInRange(a, lo, mid));
assert(_isSortedInRange(a, mid+1, hi));
for (int k = lo; k <= hi; k++) {
aux[k] = a[k];
}
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++) {
if (i > mid) {
a[k] = aux[j++];
} else if (j > hi) {
a[k] = aux[i++];
} else if (_less(aux[j], aux[i])) {
a[k] = aux[j++];
} else {
a[k] = aux[i++];
}
}
assert(_isSortedInRange(a, lo, hi));
}
bool _less(Comparable v, Comparable w) {
return (v.compareTo(w) < 0);
}
bool _isSorted(List<Comparable> a) {
return _isSortedInRange(a, 0, a.length-1);
}
bool _isSortedInRange(List<Comparable> a, int lo, int hi) {
for (int i= lo + 1; i <= hi; i++) {
if (_less(a[i], a[i-1])) {
return false;
}
}
return true;
}