quicksort

This commit is contained in:
Joachim Schoeberl 2009-10-28 00:22:10 +00:00
parent 94a06d8cae
commit 20c2f2757e
3 changed files with 55 additions and 55 deletions

View File

@ -612,9 +612,9 @@ namespace netgen
template <class T, class S> template <class T, class S>
void QickSortRec (FlatArray<T> & data, void QuickSortRec (FlatArray<T> & data,
FlatArray<T> & slave, FlatArray<S> & slave,
int left, int right) int left, int right)
{ {
int i = left; int i = left;
int j = right; int j = right;
@ -633,14 +633,14 @@ namespace netgen
} }
} }
while (i <= j); while (i <= j);
if (left < j) QickSortRec (data, slave, left, j); if (left < j) QuickSortRec (data, slave, left, j);
if (i < right) QickSortRec (data, slave, i, right); if (i < right) QuickSortRec (data, slave, i, right);
} }
template <class T, class S> template <class T, class S>
void QickSort (FlatArray<T> & data, FlatArray<S> & slave) void QuickSort (FlatArray<T> & data, FlatArray<S> & slave)
{ {
QickSortRec (data, slave, 0, data.Size()-1); QuickSortRec (data, slave, 0, data.Size()-1);
} }

View File

@ -16,60 +16,60 @@
namespace netgen namespace netgen
{ {
void Sort (const Array<double> & values, void Sort (const Array<double> & values,
Array<int> & order) Array<int> & order)
{ {
int n = values.Size(); int n = values.Size();
int i, j; int i, j;
order.SetSize (n); order.SetSize (n);
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
order.Elem(i) = i; order.Elem(i) = i;
for (i = 1; i <= n-1; i++) for (i = 1; i <= n-1; i++)
for (j = 1; j <= n-1; j++) for (j = 1; j <= n-1; j++)
if (values.Get(order.Elem(j)) > values.Get(order.Elem(j+1))) if (values.Get(order.Elem(j)) > values.Get(order.Elem(j+1)))
{ {
Swap (order.Elem(j), order.Elem(j+1)); Swap (order.Elem(j), order.Elem(j+1));
} }
} }
void QickSortRec (const Array<double> & values, void QuickSortRec (const Array<double> & values,
Array<int> & order, Array<int> & order,
int left, int right) int left, int right)
{ {
int i, j; int i, j;
double midval; double midval;
i = left; i = left;
j = right; j = right;
midval = values.Get(order.Get((i+j)/2)); midval = values.Get(order.Get((i+j)/2));
do do
{ {
while (values.Get(order.Get(i)) < midval) i++; while (values.Get(order.Get(i)) < midval) i++;
while (midval < values.Get(order.Get(j))) j--; while (midval < values.Get(order.Get(j))) j--;
if (i <= j) if (i <= j)
{ {
Swap (order.Elem(i), order.Elem(j)); Swap (order.Elem(i), order.Elem(j));
i++; j--; i++; j--;
} }
} }
while (i <= j); while (i <= j);
if (left < j) QickSortRec (values, order, left, j); if (left < j) QuickSortRec (values, order, left, j);
if (i < right) QickSortRec (values, order, i, right); if (i < right) QuickSortRec (values, order, i, right);
} }
void QickSort (const Array<double> & values, void QuickSort (const Array<double> & values,
Array<int> & order) Array<int> & order)
{ {
int i, n = values.Size(); int i, n = values.Size();
order.SetSize (n); order.SetSize (n);
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
order.Elem(i) = i; order.Elem(i) = i;
QickSortRec (values, order, 1, order.Size()); QuickSortRec (values, order, 1, order.Size());
} }
} }

View File

@ -14,7 +14,7 @@ namespace netgen
extern void Sort (const Array<double> & values, extern void Sort (const Array<double> & values,
Array<int> & order); Array<int> & order);
extern void QickSort (const Array<double> & values, extern void QuickSort (const Array<double> & values,
Array<int> & order); Array<int> & order);