diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /appl/lib/sort.b | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'appl/lib/sort.b')
| -rw-r--r-- | appl/lib/sort.b | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/appl/lib/sort.b b/appl/lib/sort.b new file mode 100644 index 00000000..a425845e --- /dev/null +++ b/appl/lib/sort.b @@ -0,0 +1,36 @@ +implement Sort; +include "sort.m"; + +sort[S, T](s: S, a: array of T) + for{ + S => + gt: fn(s: self S, x, y: T): int; + } +{ + mergesort(s, a, array[len a] of T); +} + +mergesort[S, T](s: S, a, b: array of T) + for{ + S => + gt: fn(s: self S, x, y: T): int; + } +{ + r := len a; + if (r > 1) { + m := (r-1)/2 + 1; + mergesort(s, a[0:m], b[0:m]); + mergesort(s, a[m:], b[m:]); + b[0:] = a; + for ((i, j, k) := (0, m, 0); i < m && j < r; k++) { + if(s.gt(b[i], b[j])) + a[k] = b[j++]; + else + a[k] = b[i++]; + } + if (i < m) + a[k:] = b[i:m]; + else if (j < r) + a[k:] = b[j:r]; + } +} |
