Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563680 views
1
#include"typedef.h"
2
/**************************************************************************\
3
@---------------------------------------------------------------------------
4
@---------------------------------------------------------------------------
5
@ FILE: quicksort.c
6
@---------------------------------------------------------------------------
7
@---------------------------------------------------------------------------
8
@
9
\**************************************************************************/
10
/**********************************************************************\
11
@ standard quicksort algorithms
12
@ the are called like 'quicksort(V, 0, n-1, comp)
13
@ where V is a pointer to n objects and comp is a function that
14
@ compares the objects.
15
\**********************************************************************/
16
17
18
19
20
/**************************************************************************\
21
@---------------------------------------------------------------------------
22
@ void mat_quicksort(M, inf, sup, comp)
23
@ matrix_TYP **M;
24
@ int inf, sup, (*comp)();
25
@
26
@ sorts a list of matrices M from M[inf] to M[sup] with respect to comp.
27
@---------------------------------------------------------------------------
28
@
29
\**************************************************************************/
30
void mat_quicksort(M, inf, sup, comp)
31
matrix_TYP **M;
32
int inf, sup, (*comp)();
33
{
34
int low, med, high;
35
matrix_TYP *tmp;
36
37
if(inf+1 < sup)
38
{
39
/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */
40
med = (inf+1+sup)/2;
41
tmp = M[inf];
42
M[inf] = M[med];
43
M[med] = tmp;
44
low = inf+1;
45
high = sup;
46
while(low < high)
47
{
48
while(comp(M[inf], M[low]) >= 0 && low < sup)
49
++low;
50
while(comp(M[inf], M[high]) <= 0 && high > inf)
51
--high;
52
if(low < high)
53
{
54
tmp = M[high];
55
M[high] = M[low];
56
M[low] = tmp;
57
}
58
}
59
if(inf != high)
60
{
61
tmp = M[high];
62
M[high] = M[inf];
63
M[inf] = tmp;
64
}
65
mat_quicksort(M, inf, high-1, comp);
66
mat_quicksort(M, high+1, sup, comp);
67
}
68
if(inf+1 == sup)
69
{
70
if(comp(M[inf], M[sup]) == 1)
71
{
72
tmp = M[inf];
73
M[inf] = M[sup];
74
M[sup] = tmp;
75
}
76
}
77
}
78
79
/**************************************************************************\
80
@---------------------------------------------------------------------------
81
@ void vec_quicksort(v, inf, sup, dim, comp)
82
@ int **v;
83
@ int inf, sup, (*comp)(), dim;
84
@
85
@ sorts a list of vectors v from v[inf] to v[sup] with respect to comp.
86
@---------------------------------------------------------------------------
87
@
88
\**************************************************************************/
89
void vec_quicksort(v, inf, sup, dim, comp)
90
int **v;
91
int inf, sup, (*comp)(), dim;
92
{
93
int low, med, high;
94
int *tmp;
95
96
if(inf+1 < sup)
97
{
98
/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */
99
med = (inf+1+sup)/2;
100
tmp = v[inf];
101
v[inf] = v[med];
102
v[med] = tmp;
103
low = inf+1;
104
high = sup;
105
while(low < high)
106
{
107
while(comp(v[inf], v[low], dim) >= 0 && low < sup)
108
++low;
109
while(comp(v[inf], v[high], dim) <= 0 && high > inf)
110
--high;
111
if(low < high)
112
{
113
tmp = v[high];
114
v[high] = v[low];
115
v[low] = tmp;
116
}
117
}
118
if(inf != high)
119
{
120
tmp = v[high];
121
v[high] = v[inf];
122
v[inf] = tmp;
123
}
124
vec_quicksort(v, inf, high-1, dim, comp);
125
vec_quicksort(v, high+1, sup, dim, comp);
126
}
127
if(inf+1 == sup)
128
{
129
if(comp(v[inf], v[sup], dim) == 1)
130
{
131
tmp = v[inf];
132
v[inf] = v[sup];
133
v[sup] = tmp;
134
}
135
}
136
}
137
138
139
/**************************************************************************\
140
@---------------------------------------------------------------------------
141
@ void pointer_mat_quicksort(v, inf, sup, rows, cols, comp)
142
@ int ***v;
143
@ int inf, sup, rows, cols, (*comp)();
144
@
145
@---------------------------------------------------------------------------
146
@
147
@ sorts a list of 2-dimensional vecotrs v from v[inf] to v[sup]
148
@ with respect to comp.
149
\**************************************************************************/
150
void pointer_mat_quicksort(v, inf, sup, rows, cols, comp)
151
int ***v;
152
int inf, sup, rows, cols, (*comp)();
153
{
154
int low, med, high;
155
int **tmp;
156
157
if(inf+1 < sup)
158
{
159
/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */
160
med = (inf+1+sup)/2;
161
tmp = v[inf];
162
v[inf] = v[med];
163
v[med] = tmp;
164
low = inf+1;
165
high = sup;
166
while(low < high)
167
{
168
while(comp(v[inf], v[low], rows, cols) >= 0 && low < sup)
169
++low;
170
while(comp(v[inf], v[high], rows, cols) <= 0 && high > inf)
171
--high;
172
if(low < high)
173
{
174
tmp = v[high];
175
v[high] = v[low];
176
v[low] = tmp;
177
}
178
}
179
if(inf != high)
180
{
181
tmp = v[high];
182
v[high] = v[inf];
183
v[inf] = tmp;
184
}
185
pointer_mat_quicksort(v, inf, high-1, rows, cols, comp);
186
pointer_mat_quicksort(v, high+1, sup, rows, cols, comp);
187
}
188
if(inf+1 == sup)
189
{
190
if(comp(v[inf], v[sup], rows, cols) == 1)
191
{
192
tmp = v[inf];
193
v[inf] = v[sup];
194
v[sup] = tmp;
195
}
196
}
197
}
198
199