Fast Auxiliary Space Preconditioning 2.7.7 Aug/28/2022
Loading...
Searching...
No Matches
AuxSort.c
Go to the documentation of this file.
1
14#include "fasp.h"
15#include "fasp_functs.h"
16
17/*---------------------------------*/
18/*-- Declare Private Functions --*/
19/*---------------------------------*/
20
21static void dSwapping (REAL *w, const INT i, const INT j);
22static void iSwapping (INT *w, const INT i, const INT j);
23
24/*---------------------------------*/
25/*-- Public Functions --*/
26/*---------------------------------*/
27
43 const INT *list,
44 const INT value)
45{
46 INT low, high, m;
47
48 low = 0;
49 high = nlist - 1;
50
51 while (low <= high) {
52 m = (low + high) / 2;
53 if (value < list[m]) {
54 high = m - 1;
55 }
56 else if (value > list[m]) {
57 low = m + 1;
58 }
59 else {
60 return m;
61 }
62 }
63
64 return -1;
65}
66
83 const INT size)
84{
85 INT i, newsize;
86
87 if ( size == 0 ) return(0);
88
89 for ( newsize = 0, i = 1; i < size; ++i ) {
90 if ( numbers[newsize] < numbers[i] ) {
91 newsize++;
92 numbers[newsize] = numbers[i];
93 }
94 }
95
96 return(newsize+1);
97}
98
115void fasp_aux_merge (INT numbers[],
116 INT work[],
117 INT left,
118 INT mid,
119 INT right)
120{
121 INT i, left_end, num_elements, tmp_pos;
122
123 left_end = mid - 1;
124 tmp_pos = left;
125 num_elements = right - left + 1;
126
127 while ((left <= left_end) && (mid <= right)) {
128
129 if (numbers[left] <= numbers[mid]) // first branch <=
130 {
131 work[tmp_pos] = numbers[left];
132 tmp_pos = tmp_pos + 1;
133 left = left +1;
134 }
135 else // second branch >
136 {
137 work[tmp_pos] = numbers[mid];
138 tmp_pos = tmp_pos + 1;
139 mid = mid + 1;
140 }
141 }
142
143 while (left <= left_end) {
144 work[tmp_pos] = numbers[left];
145 left = left + 1;
146 tmp_pos = tmp_pos + 1;
147 }
148
149 while (mid <= right) {
150 work[tmp_pos] = numbers[mid];
151 mid = mid + 1;
152 tmp_pos = tmp_pos + 1;
153 }
154
155 for (i = 0; i < num_elements; ++i) {
156 numbers[right] = work[right];
157 right = right - 1;
158 }
159
160}
161
177void fasp_aux_msort (INT numbers[],
178 INT work[],
179 INT left,
180 INT right)
181{
182 INT mid;
183
184 if (right > left) {
185 mid = (right + left) / 2;
186 fasp_aux_msort(numbers, work, left, mid);
187 fasp_aux_msort(numbers, work, mid+1, right);
188 fasp_aux_merge(numbers, work, left, mid+1, right);
189 }
190
191}
192
209 INT left,
210 INT right)
211{
212 INT i, last;
213
214 if (left >= right) return;
215
216 iSwapping(a, left, (left+right)/2);
217
218 last = left;
219 for (i = left+1; i <= right; ++i) {
220 if (a[i] < a[left]) {
221 iSwapping(a, ++last, i);
222 }
223 }
224
225 iSwapping(a, left, last);
226
227 fasp_aux_iQuickSort(a, left, last-1);
228 fasp_aux_iQuickSort(a, last+1, right);
229}
230
247 INT left,
248 INT right)
249{
250 INT i, last;
251
252 if (left >= right) return;
253
254 dSwapping(a, left, (left+right)/2);
255
256 last = left;
257 for (i = left+1; i <= right; ++i) {
258 if (a[i] < a[left]) {
259 dSwapping(a, ++last, i);
260 }
261 }
262
263 dSwapping(a, left, last);
264
265 fasp_aux_dQuickSort(a, left, last-1);
266 fasp_aux_dQuickSort(a, last+1, right);
267}
268
287 INT left,
288 INT right,
289 INT *index)
290{
291 INT i, last;
292
293 if (left >= right) return;
294
295 iSwapping(index, left, (left+right)/2);
296
297 last = left;
298 for (i = left+1; i <= right; ++i) {
299 if (a[index[i]] < a[index[left]]) {
300 iSwapping(index, ++last, i);
301 }
302 }
303
304 iSwapping(index, left, last);
305
306 fasp_aux_iQuickSortIndex(a, left, last-1, index);
307 fasp_aux_iQuickSortIndex(a, last+1, right, index);
308}
309
328 INT left,
329 INT right,
330 INT *index)
331{
332 INT i, last;
333
334 if (left >= right) return;
335
336 iSwapping(index, left, (left+right)/2);
337
338 last = left;
339 for (i = left+1; i <= right; ++i) {
340 if (a[index[i]] < a[index[left]]) {
341 iSwapping(index, ++last, i);
342 }
343 }
344
345 iSwapping(index, left, last);
346
347 fasp_aux_dQuickSortIndex(a, left, last-1, index);
348 fasp_aux_dQuickSortIndex(a, last+1, right, index);
349}
350
351/*---------------------------------*/
352/*-- Private Functions --*/
353/*---------------------------------*/
354
367static void iSwapping (INT *w,
368 const INT i,
369 const INT j)
370{
371 const INT temp = w[i];
372 w[i] = w[j]; w[j] = temp;
373}
374
387static void dSwapping (REAL *w,
388 const INT i,
389 const INT j)
390{
391 const REAL temp = w[i];
392 w[i] = w[j]; w[j] = temp;
393}
394
395/*---------------------------------*/
396/*-- End of File --*/
397/*---------------------------------*/
INT fasp_aux_BiSearch(const INT nlist, const INT *list, const INT value)
Binary Search.
Definition: AuxSort.c:42
void fasp_aux_iQuickSortIndex(INT *a, INT left, INT right, INT *index)
Reorder the index of (INT type) so that 'a' is in ascending order.
Definition: AuxSort.c:286
void fasp_aux_merge(INT numbers[], INT work[], INT left, INT mid, INT right)
Merge two sorted arrays.
Definition: AuxSort.c:115
void fasp_aux_msort(INT numbers[], INT work[], INT left, INT right)
Sort the INT array in ascending order with the merge sort algorithm.
Definition: AuxSort.c:177
void fasp_aux_dQuickSortIndex(REAL *a, INT left, INT right, INT *index)
Reorder the index of (REAL type) so that 'a' is ascending in such order.
Definition: AuxSort.c:327
void fasp_aux_dQuickSort(REAL *a, INT left, INT right)
Sort the array (REAL type) in ascending order with the quick sorting algorithm.
Definition: AuxSort.c:246
void fasp_aux_iQuickSort(INT *a, INT left, INT right)
Sort the array (INT type) in ascending order with the quick sorting algorithm.
Definition: AuxSort.c:208
INT fasp_aux_unique(INT numbers[], const INT size)
Remove duplicates in an sorted (ascending order) array.
Definition: AuxSort.c:82
Main header file for the FASP project.
#define REAL
Definition: fasp.h:75
#define INT
Definition: fasp.h:72