16 #define REPORT { static ExeCounter ExeCount(__LINE__,13); ++ExeCount; } 32 #define DoSimpleSort 17 // when to switch to insert sort 33 #define MaxDepth 50 // maximum recursion depth 35 static void MyQuickSortDescending(
Real* first,
Real* last,
int depth);
36 static void InsertionSortDescending(
Real* first,
const int length,
40 static void MyQuickSortAscending(
Real* first,
Real* last,
int depth);
41 static void InsertionSortAscending(
Real* first,
const int length,
48 Tracer et(
"QuickSortDescending");
62 if (*b >= *c) {
REPORT return *b; }
63 else if (*a >= *c) {
REPORT Real x = *c; *c = *b; *b =
x;
return x; }
64 else {
REPORT Real x = *a; *a = *c; *c = *b; *b =
x;
return x; }
66 else if (*c >= *b) {
REPORT Real x = *c; *c = *a; *a =
x;
return *b; }
67 else if (*a >= *c) {
REPORT Real x = *a; *a = *b; *b =
x;
return x; }
68 else {
REPORT Real x = *c; *c = *a; *a = *b; *b =
x;
return x; }
71 static void InsertionSortDescending(
Real* first,
const int length,
77 if (length <= 1)
return;
81 if (guard > length) {
REPORT guard = length; }
83 while (i--)
if (v < *(++f)) { v = *f; h = f; }
84 *h = *first; *first = v;
87 i = length - 1; f = first;
90 Real* g = f++; h = f; v = *h;
91 while (*g < v) *h-- = *g--;
96 static void MyQuickSortDescending(
Real* first,
Real* last,
int depth)
101 const int length = last - first + 1;
105 Real* centre = first + length/2;
106 const Real test = SortThreeDescending(first, centre, last);
110 while (*(++f) > test) {}
111 while (*(--l) < test) {}
113 const Real temp = *f; *f = *l; *l = temp;
116 {
REPORT MyQuickSortDescending(l+1, last, depth); last = f-1; }
117 else {
REPORT MyQuickSortDescending(first, f-1, depth); first = l+1; }
124 Tracer et(
"QuickSortAscending");
133 static void InsertionSortAscending(
Real* first,
const int length,
139 if (length <= 1)
return;
143 if (guard > length) {
REPORT guard = length; }
145 while (i--)
if (v > *(++f)) { v = *f; h = f; }
146 *h = *first; *first = v;
149 i = length - 1; f = first;
152 Real* g = f++; h = f; v = *h;
153 while (*g > v) *h-- = *g--;
157 static void MyQuickSortAscending(
Real* first,
Real* last,
int depth)
162 const int length = last - first + 1;
166 Real* centre = first + length/2;
167 const Real test = SortThreeDescending(last, centre, first);
171 while (*(++f) < test) {}
172 while (*(--l) > test) {}
174 const Real temp = *f; *f = *l; *l = temp;
177 {
REPORT MyQuickSortAscending(l+1, last, depth); last = f-1; }
178 else {
REPORT MyQuickSortAscending(first, f-1, depth); first = l+1; }
193 Tracer trace(
"SortSV_DU");
197 for (
int i=0; i<
n; i++)
202 for (
int j=i+1; j<
n; j++)
207 for (
int j=i+1; j<
n; j++)
213 Real* uji = u + i;
Real* ujk = u + k;
216 p = *uji; *uji = *ujk; *ujk = p;
227 Tracer trace(
"SortSV_DUV");
232 for (
int i=0; i<
n; i++)
237 for (
int j=i+1; j<
n; j++)
242 for (
int j=i+1; j<
n; j++)
248 Real* uji = u + i;
Real* ujk = u + k;
249 Real* vji = v + i;
Real* vjk = v + k;
253 p = *uji; *uji = *ujk; *ujk = p;
if (!(--j))
break;
259 p = *vji; *vji = *vjk; *vjk = p;
if (!(--j))
break;
void SortDescending(GeneralMatrix &GM)
void SortAscending(GeneralMatrix &GM)
os2<< "> n<< " > nendobj n
void SortSV(DiagonalMatrix &D, Matrix &U, bool ascending)