Lolly 1.4.27
Loading...
Searching...
No Matches
Classes | Functions
merge_sort.hpp File Reference
#include "array.hpp"
#include "hashmap.hpp"
Include dependency graph for merge_sort.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  less_eq_operator< T >
 

Functions

template<class T , class LEQ >
static void merge_sort_sub (array< T > &a, int start, int end, array< T > &merge_buf)
 
template<class T , class LEQ >
void merge_sort_leq (array< T > &a)
 
template<class T >
void merge_sort (array< T > &a)
 
template<class T , class U , class LEQ >
static void merge_sort_sub (array< T > &a, array< U > &b, int start, int end, array< T > &ma, array< U > &mb)
 
template<class T , class U , class LEQ >
void merge_sort_leq (array< T > &a, array< U > &b)
 
template<class T , class LEQ >
array< int > merge_sort_leq_permutation (array< T > &a)
 
template<class T >
array< int > merge_sort_leq_permutation (array< T > &a)
 
template<class T >
array< T > permute (array< T > &a, array< int > b)
 

Function Documentation

◆ merge_sort_sub() [1/2]

template<class T , class LEQ >
static void merge_sort_sub ( array< T > & a,
int start,
int end,
array< T > & merge_buf )
static

Definition at line 27 of file merge_sort.hpp.

27 {
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}
The list class represents a linked list.
Definition list.hpp:48

◆ merge_sort_leq() [1/2]

template<class T , class LEQ >
void merge_sort_leq ( array< T > & a)

Definition at line 53 of file merge_sort.hpp.

53 {
54 array<T> merge_buf (N (a));
56}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170

◆ merge_sort()

template<class T >
void merge_sort ( array< T > & a)
inline

Definition at line 60 of file merge_sort.hpp.

◆ merge_sort_sub() [2/2]

template<class T , class U , class LEQ >
static void merge_sort_sub ( array< T > & a,
array< U > & b,
int start,
int end,
array< T > & ma,
array< U > & mb )
static

Definition at line 70 of file merge_sort.hpp.

71 {
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}
blackbox b[13]

◆ merge_sort_leq() [2/2]

template<class T , class U , class LEQ >
void merge_sort_leq ( array< T > & a,
array< U > & b )

Definition at line 110 of file merge_sort.hpp.

110 {
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}
#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

◆ merge_sort_leq_permutation() [1/2]

template<class T , class LEQ >
array< int > merge_sort_leq_permutation ( array< T > & a)

Definition at line 119 of file merge_sort.hpp.

119 {
120 array<int> b (N (a));
121 for (int i= 0; i < N (a); i++)
122 b[i]= i;
124 return b;
125}

◆ merge_sort_leq_permutation() [2/2]

template<class T >
array< int > merge_sort_leq_permutation ( array< T > & a)

Definition at line 129 of file merge_sort.hpp.

◆ permute()

template<class T >
array< T > permute ( array< T > & a,
array< int > b )

Definition at line 135 of file merge_sort.hpp.

135 {
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}