quicksort for arrays

This commit is contained in:
Joachim Schoeberl 2009-10-28 00:04:19 +00:00
parent e2f16032cc
commit 94a06d8cae

View File

@ -582,37 +582,75 @@ inline ostream & operator<< (ostream & ost, MoveableArray<T> & a)
template <class T>
inline void BubbleSort (const FlatArray<T> & data)
{
T hv;
for (int i = 0; i < data.Size(); i++)
for (int j = i+1; j < data.Size(); j++)
if (data[i] > data[j])
{
hv = data[i];
T hv = data[i];
data[i] = data[j];
data[j] = hv;
}
}
/// bubble sort array
template <class T, class S>
inline void BubbleSort (FlatArray<T> & data, FlatArray<S> & slave)
{
T hv;
S hvs;
for (int i = 0; i < data.Size(); i++)
for (int j = i+1; j < data.Size(); j++)
if (data[i] > data[j])
{
hv = data[i];
T hv = data[i];
data[i] = data[j];
data[j] = hv;
hvs = slave[i];
S hvs = slave[i];
slave[i] = slave[j];
slave[j] = hvs;
}
}
template <class T, class S>
void QickSortRec (FlatArray<T> & data,
FlatArray<T> & slave,
int left, int right)
{
int i = left;
int j = right;
T midval = data[(left+right)/2];
do
{
while (data[i] < midval) i++;
while (midval < data[j]) j--;
if (i <= j)
{
Swap (data[i], data[j]);
Swap (slave[i], slave[j]);
i++; j--;
}
}
while (i <= j);
if (left < j) QickSortRec (data, slave, left, j);
if (i < right) QickSortRec (data, slave, i, right);
}
template <class T, class S>
void QickSort (FlatArray<T> & data, FlatArray<S> & slave)
{
QickSortRec (data, slave, 0, data.Size()-1);
}
template <class T>
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2,
Array<T> & out)