Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/sort.cpp
3206 views
1
/**************************************************************************/
2
/* File: sort.cc */
3
/* Author: Joachim Schoeberl */
4
/* Date: 07. Jan. 00 */
5
/**************************************************************************/
6
7
/*
8
Sorting
9
*/
10
11
12
#include <algorithm>
13
#include <mystdlib.h>
14
#include <myadt.hpp>
15
16
namespace netgen
17
{
18
19
void Sort (const ARRAY<double> & values,
20
ARRAY<int> & order)
21
{
22
int n = values.Size();
23
int i, j;
24
25
order.SetSize (n);
26
27
for (i = 1; i <= n; i++)
28
order.Elem(i) = i;
29
for (i = 1; i <= n-1; i++)
30
for (j = 1; j <= n-1; j++)
31
if (values.Get(order.Elem(j)) > values.Get(order.Elem(j+1)))
32
{
33
Swap (order.Elem(j), order.Elem(j+1));
34
}
35
}
36
37
38
void QickSortRec (const ARRAY<double> & values,
39
ARRAY<int> & order,
40
int left, int right)
41
{
42
int i, j;
43
double midval;
44
45
i = left;
46
j = right;
47
midval = values.Get(order.Get((i+j)/2));
48
49
do
50
{
51
while (values.Get(order.Get(i)) < midval) i++;
52
while (midval < values.Get(order.Get(j))) j--;
53
54
if (i <= j)
55
{
56
Swap (order.Elem(i), order.Elem(j));
57
i++; j--;
58
}
59
}
60
while (i <= j);
61
if (left < j) QickSortRec (values, order, left, j);
62
if (i < right) QickSortRec (values, order, i, right);
63
}
64
65
void QickSort (const ARRAY<double> & values,
66
ARRAY<int> & order)
67
{
68
int i, n = values.Size();
69
order.SetSize (n);
70
for (i = 1; i <= n; i++)
71
order.Elem(i) = i;
72
73
QickSortRec (values, order, 1, order.Size());
74
}
75
}
76
77