2009-01-13 04:40:13 +05:00
|
|
|
/**************************************************************************/
|
|
|
|
/* File: sort.cc */
|
|
|
|
/* Author: Joachim Schoeberl */
|
|
|
|
/* Date: 07. Jan. 00 */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Sorting
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <mystdlib.h>
|
|
|
|
#include <myadt.hpp>
|
|
|
|
|
|
|
|
namespace netgen
|
|
|
|
{
|
|
|
|
|
2019-07-09 13:39:16 +05:00
|
|
|
void Sort (const NgArray<double> & values,
|
|
|
|
NgArray<int> & order)
|
2009-10-28 05:22:10 +05:00
|
|
|
{
|
|
|
|
int n = values.Size();
|
|
|
|
int i, j;
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:22:10 +05:00
|
|
|
order.SetSize (n);
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:22:10 +05:00
|
|
|
for (i = 1; i <= n; i++)
|
|
|
|
order.Elem(i) = i;
|
|
|
|
for (i = 1; i <= n-1; i++)
|
|
|
|
for (j = 1; j <= n-1; j++)
|
|
|
|
if (values.Get(order.Elem(j)) > values.Get(order.Elem(j+1)))
|
|
|
|
{
|
|
|
|
Swap (order.Elem(j), order.Elem(j+1));
|
|
|
|
}
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
2019-07-09 13:39:16 +05:00
|
|
|
void QuickSortRec (const NgArray<double> & values,
|
|
|
|
NgArray<int> & order,
|
2009-10-28 05:22:10 +05:00
|
|
|
int left, int right)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
double midval;
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:22:10 +05:00
|
|
|
i = left;
|
|
|
|
j = right;
|
|
|
|
midval = values.Get(order.Get((i+j)/2));
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:22:10 +05:00
|
|
|
do
|
|
|
|
{
|
|
|
|
while (values.Get(order.Get(i)) < midval) i++;
|
|
|
|
while (midval < values.Get(order.Get(j))) j--;
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:22:10 +05:00
|
|
|
if (i <= j)
|
|
|
|
{
|
|
|
|
Swap (order.Elem(i), order.Elem(j));
|
|
|
|
i++; j--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (i <= j);
|
|
|
|
if (left < j) QuickSortRec (values, order, left, j);
|
|
|
|
if (i < right) QuickSortRec (values, order, i, right);
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2019-07-09 13:39:16 +05:00
|
|
|
void QuickSort (const NgArray<double> & values,
|
|
|
|
NgArray<int> & order)
|
2009-10-28 05:22:10 +05:00
|
|
|
{
|
|
|
|
int i, n = values.Size();
|
|
|
|
order.SetSize (n);
|
|
|
|
for (i = 1; i <= n; i++)
|
|
|
|
order.Elem(i) = i;
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:22:10 +05:00
|
|
|
QuickSortRec (values, order, 1, order.Size());
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
}
|