# Include <iostream>
# Include <vector>
# Include <iterator>
# Include <algorithm>
# Include <time. h>
# Include <stdlib. h>
Using namespace STD;
Template <class T>
Class CMP // Abstract Operator class
{
Public:
Virtual bool operator () (const T & one, const T & Other) = 0; // pure virtual function, overload operator ()
};
Template <class T>
Class lessthan: Public CMP <t> // less than operator class
{
Public:
Bool operator () (const T & one, const T & other)
{
Return one <Other;
}
};
Template <class T>
Class greatthan: Public CMP <t>
{
Public:
Bool operator () (const T & one, const T & other)
{
Return one> Other;
}
};
Template <class iterator, class T> // type of the elements to be sorted
Class sort // abstract sorting class
{
Protected:
Iterator array; // The start pointer to the interval to be sorted.
Public:
Virtual void operator () (const iterator & beg, const iterator & End, CMP <t> & CMP) = 0;
};
Template <class iterator, class T>
Class quicksort: Public sort <iterator, T>
{
Protected:
Int low, high; // point to the start and end of the sorting Interval
Int partition (INT low, int high, CMP <t> & CMP); // set the interval [low... high] is divided into [low... pipeline], [pipeline + 1... high]
Int randpos (INT low, int high); // return a random position between low... high
Void Exchange (int I, Int J); // exchange two elements of position I and position J.
Void sort_function (INT low, int high, CMP <t> & CMP); // merge and sort the interval [low... high]
Public:
Void operator () (const iterator & beg, const iterator & End, CMP <t> & CMP)
{
Low = 0;
High = end-Beg-1;
Array = beg;
Sort_function (low, high, CMP );
}
};
Template <class iterator, class T>
Class selectkmin: Public quicksort <iterator, T> // select the K smallest element at random.
{
PRIVATE:
Int K; // minimum K
T kmin; // minimum element of K
Void selectkmin (INT start, int end, int K, CMP <t> & CMP); // select the K minimum element between start and end.
Public:
Selectkmin (int I): K (I)
{
}
Void operator () (const iterator & beg, const iterator & End, CMP <t> & CMP );
T getkmin () const
{
Return kmin;
}
};
Template <class iterator, class T>
Void selectkmin <iterator, T>: selectkmin (INT start, int end, int K, CMP <t> & CMP) // select the K minimum element between start and end.
{
Int second, I;
If (START = END) // locate
{
Kmin = array [start];
Return;
}
Partition = partition (START, end, CMP );
I = starting-start; // I indicates the I-th small element, starting from 0.
If (I = k) // already found
{
Kmin = array [minimum]; // at this time, the central element is the K smallest element.
Return;
}
Else if (k <I) selectkmin (START, sequence-1, K, CMP );
Else selectkmin (rows + 1, end, k-i-1, CMP); // The k-i-1 indicates that the minimum element of the k-i-1 is to be searched in the new interval
}
Template <class iterator, class T>
Void selectkmin <iterator, T>: Operator () (const iterator & beg, const iterator & End, CMP <t> & CMP)
{
Low = 0;
High = end-Beg-1;
Array = beg;
If (k <0 | K> high)
{
The cerr <"constructor parameter is invalid. It must be between 0 and" Exit (0 );
}
Selectkmin (low, high, K, CMP );
}
Template <class iterator, class T>
Int quicksort <iterator, T>: randpos (INT low, int high)
{
Int n = high-low + 1;
Srand (unsigned) time (0 ));
Int offset = rand () % N;
If (offset = N) offset --;
Int Pos = low + offset;
If (Pos <= high & Pos> = low) return Pos;
Else return low;
}
Template <class iterator, class T>
Void quicksort <iterator, T>: Exchange (int I, Int J)
{
T TMP;
TMP = array [I];
Array [I] = array [J];
Array [J] = TMP;
}
Template <class iterator, class T>
Int quicksort <iterator, t >:: partition (INT low, int high, CMP <t> & CMP)
{
Int Pos = randpos (low, high );
Exchange (low, POS );
Int I, J;
I = low;
J = high;
T TMP = array [low];
While (I <j)
{
While (I <J & array [J]> TMP) j --;
If (I <j) array [I ++] = array [J];
While (I <J & array [I] <TMP) I ++;
If (I <j) array [j --] = array [I];
}
Array [I] = TMP;
Return I;
}
Template <class iterator, class T>
Void quicksort <iterator, T>: sort_function (INT low, int high, CMP <t> & CMP)
{
If (low {
Int partition = partition (low, high, CMP );
Sort_function (low, skip-1, CMP );
Sort_function (cost + 1, high, CMP );
}
}
Template <class iterator, class T>
Void quick_sort (const iterator & beg, const iterator & End, CMP <t> & CMP)
{
Quicksort <iterator, T> MS;
MS (beg, end, CMP );
}
Void main ()
{
Vector <int>;
Srand (unsigned) time (0 ));
Const int n = 10;
Int X;
For (INT I = 0; I <n; I ++)
{
X = rand () % N;
A. push_back (X );
Cout <x <"";
}
Cout <Endl;
Int CNT = 0;
Int K;
Do
{
Cout <"select the minimum elements to search :";
Cin> K;
Selectkmin <vector <int >:: iterator, int> SKM (k );
SKM (A. Begin (), A. End (), lessthan <int> ());
Cout <"no." <k <"minimum element:" <SKM. getkmin () <Endl;
CNT ++;
} While (CNT <n );
}