Lolly 1.4.27
Loading...
Searching...
No Matches
merge_sort.hpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : merge_sort
4 * COPYRIGHT : (C) 1999 Joris van der Hoeven
5 *******************************************************************************
6 * This software falls under the GNU general public license version 3 or later.
7 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
8 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
9 ******************************************************************************/
10
11#ifndef MERGE_SORT_H
12#define MERGE_SORT_H
13
14#include "array.hpp"
15#include "hashmap.hpp"
16
17/******************************************************************************
18 * Basic merge sort
19 ******************************************************************************/
20
21template <class T> struct less_eq_operator {
22 static inline bool leq (T& a, T& b) { return a <= b; }
23};
24
25template <class T, class LEQ>
26static void
28 if (end - start <= 1) return;
29 if (end - start == 2) {
30 if (!LEQ::leq (a[start], a[start + 1])) {
32 a[start] = a[start + 1];
33 a[start + 1] = merge_buf[start];
34 }
35 return;
36 }
37 int middle= (start + end) >> 1;
40 int i, j, k;
41 for (i= start, j= middle, k= start; (i < middle) && (j < end);)
42 if (LEQ::leq (a[i], a[j])) merge_buf[k++]= a[i++];
43 else merge_buf[k++]= a[j++];
44 j= k;
45 while (i != middle)
46 a[k++]= a[i++];
47 for (i= start; i < j; i++)
48 a[i]= merge_buf[i];
49}
50
51template <class T, class LEQ>
52void
57
58template <class T>
59inline void
63
64/******************************************************************************
65 * Sort two arrays on the first array
66 ******************************************************************************/
67
68template <class T, class U, class LEQ>
69static void
71 array<U>& mb) {
72 if (end - start <= 1) return;
73 if (end - start == 2) {
74 if (!LEQ::leq (a[start], a[start + 1])) {
75 ma[start] = a[start];
76 a[start] = a[start + 1];
77 a[start + 1]= ma[start];
78 mb[start] = b[start];
79 b[start] = b[start + 1];
80 b[start + 1]= mb[start];
81 }
82 return;
83 }
84 int middle= (start + end) >> 1;
87 int i, j, k;
88 for (i= start, j= middle, k= start; (i < middle) && (j < end);)
89 if (LEQ::leq (a[i], a[j])) {
90 mb[k] = b[i];
91 ma[k++]= a[i++];
92 }
93 else {
94 mb[k] = b[j];
95 ma[k++]= a[j++];
96 }
97 j= k;
98 while (i != middle) {
99 b[k] = b[i];
100 a[k++]= a[i++];
101 }
102 for (i= start; i < j; i++) {
103 b[i]= mb[i];
104 a[i]= ma[i];
105 }
106}
107
108template <class T, class U, class LEQ>
109void
111 ASSERT (N (a) == N (b), "arrays of the same length expected");
112 array<T> ma (N (a));
113 array<U> mb (N (b));
114 merge_sort_sub<T, U, LEQ> (a, b, 0, N (a), ma, mb);
115}
116
117template <class T, class LEQ>
120 array<int> b (N (a));
121 for (int i= 0; i < N (a); i++)
122 b[i]= i;
124 return b;
125}
126
127template <class T>
132
133template <class T>
136 ASSERT (N (a) == N (b), "arrays of the same length expected");
137 array<T> r (N (a));
138 for (int i= 0; i < N (a); i++)
139 r[i]= a[b[i]];
140 return r;
141}
142
143#endif // MERGE_SORT_H
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
blackbox b[13]
The list class represents a linked list.
Definition list.hpp:48
array< T > permute(array< T > &a, array< int > b)
static void merge_sort_sub(array< T > &a, int start, int end, array< T > &merge_buf)
void merge_sort_leq(array< T > &a)
array< int > merge_sort_leq_permutation(array< T > &a)
void merge_sort(array< T > &a)
static bool leq(T &a, T &b)