SenK
SenK is a C++ library for high-performance linear solvers.
senk_helper.hpp
Go to the documentation of this file.
1
7#ifndef SENK_HELPER_HPP
8#define SENK_HELPER_HPP
9
10namespace senk {
14namespace helper {
21template <typename T>
22void Swap(T *a, T *b)
23{
24 T temp = a[0];
25 a[0] = b[0];
26 b[0] = temp;
27}
35template <typename T>
36void QuickSort(T *key, int left, int right)
37{
38 int Left, Right;
39 T pivot;
40 Left = left; Right = right;
41 pivot = key[(left + right) / 2];
42 while (1) {
43 while (key[Left] < pivot) Left++;
44 while (pivot < key[Right]) Right--;
45 if (Left >= Right) break;
46 Swap<T>(&key[Left], &key[Right]);
47 Left++; Right--;
48 }
49 if (left < Left-1) QuickSort<T>(key, left, Left-1);
50 if (Right+1 < right) QuickSort<T>(key, Right+1, right);
51}
61template <typename T, typename T2>
62void QuickSort(T *key, T2 *sub, int left, int right)
63{
64 int Left, Right;
65 T pivot;
66 Left = left; Right = right;
67 pivot = key[(left + right) / 2];
68 while (1) {
69 while (key[Left] < pivot) Left++;
70 while (pivot < key[Right]) Right--;
71 if (Left >= Right) break;
72 Swap<T>(&key[Left], &key[Right]);
73 Swap<T2>(&sub[Left], &sub[Right]);
74 Left++; Right--;
75 }
76 if (left < Left-1) QuickSort<T, T2>(key, sub, left, Left-1);
77 if (Right+1 < right) QuickSort<T, T2>(key, sub, Right+1, right);
78}
90template <typename T, typename T2, typename T3>
91void QuickSort(T *key, T2 *sub, T3 *sub2, int left, int right)
92{
93 int Left, Right;
94 T pivot;
95 Left = left; Right = right;
96 pivot = key[(left + right) / 2];
97 while (1) {
98 while (key[Left] < pivot) Left++;
99 while (pivot < key[Right]) Right--;
100 if (Left >= Right) break;
101 Swap<T>(&key[Left], &key[Right]);
102 Swap<T2>(&sub[Left], &sub[Right]);
103 Swap<T3>(&sub2[Left], &sub2[Right]);
104 Left++; Right--;
105 }
106 if (left < Left-1) QuickSort<T, T2, T3>(key, sub, sub2, left, Left-1);
107 if (Right+1 < right) QuickSort<T, T2, T3>(key, sub, sub2, Right+1, right);
108}
116template <typename T>
117void QuickSortDesc(T *key, int left, int right)
118{
119 int Left, Right;
120 T pivot;
121 Left = left; Right = right;
122 pivot = key[(left + right) / 2];
123 while (1) {
124 while (key[Left] > pivot) Left++;
125 while (pivot > key[Right]) Right--;
126 if (Left >= Right) break;
127 Swap<T>(&key[Left], &key[Right]);
128 Left++; Right--;
129 }
130 if (left < Left-1) QuickSortDesc<T>(key, left, Left-1);
131 if (Right+1 < right) QuickSortDesc<T>(key, Right+1, right);
132}
142template <typename T, typename T2>
143void QuickSortDesc(T *key, T2 *sub, int left, int right)
144{
145 int Left, Right;
146 T pivot;
147 Left = left; Right = right;
148 pivot = key[(left + right) / 2];
149 while (1) {
150 while (key[Left] > pivot) Left++;
151 while (pivot > key[Right]) Right--;
152 if (Left >= Right) break;
153 Swap<T>(&key[Left], &key[Right]);
154 Swap<T2>(&sub[Left], &sub[Right]);
155 Left++; Right--;
156 }
157 if (left < Left-1) QuickSortDesc<T, T2>(key, sub, left, Left-1);
158 if (Right+1 < right) QuickSortDesc<T, T2>(key, sub, Right+1, right);
159}
171template <typename T, typename T2, typename T3>
172void QuickSortDesc(T *key, T2 *sub, T3 *sub2, int left, int right)
173{
174 int Left, Right;
175 T pivot;
176 Left = left; Right = right;
177 pivot = key[(left + right) / 2];
178 while (1) {
179 while (key[Left] > pivot) Left++;
180 while (pivot > key[Right]) Right--;
181 if (Left >= Right) break;
182 Swap<T>(&key[Left], &key[Right]);
183 Swap<T2>(&sub[Left], &sub[Right]);
184 Swap<T3>(&sub2[Left], &sub2[Right]);
185 Left++; Right--;
186 }
187 if (left < Left-1) QuickSortDesc<T, T2, T3>(key, sub, sub2, left, Left-1);
188 if (Right+1 < right) QuickSortDesc<T, T2, T3>(key, sub, sub2, Right+1, right);
189}
190/*
191template <typename T>
192T Sqrt(T x)
193{
194 T s, t;
195 if(x <= 0) return 0;
196 s = 1; t = x;
197 while(s < t) { s<<=1; t>>=1; }
198 do { t = s; s=(x/s+s)>>1; } while (s < t);
199 return t;
200}
201*/
202} // namespace helper
203
204} // namespace senk
205
206#endif
void QuickSortDesc(T *key, int left, int right)
Sort an array in descending order by the quick sort.
void QuickSort(T *key, int left, int right)
Sort an array by the quick sort.
Definition: senk_helper.hpp:36
void Swap(T *a, T *b)
Swap two variables.
Definition: senk_helper.hpp:22
The top-level namespace of SenK.