I'm trying to learn algorithm and get better with C++. So instead of using all the sorting procedure comes with STL, I have learn to write a few sorting codes. Now I want to merge them together. I'm working on two right now, merging the heap sort and quick sort together.
Code:
template <class Type>
class Heap
{
private:
Type *A;
int heap_size;
int heap_length;
public:
//Heap();
Heap(Type * array, int length);
~Heap();
int parent(int i) { return i/2;};
int left (int j) { return 2*j+1;};
int right (int k) { return 2*k+2;};
void exch(double *x, double *y);
void show();
void max_heapify(int i);
void build_max_heap();
void sort();
Type heap_max() {return A[0];};
double heap_extract_max();
};
#endif
Code:
template<class Type>
class Qsort
{
private:
Type *A;
int length;
public:
Qsort();
Qsort( int n, Type * Arr);
~Qsort();
// Quick sort
void sort(int p , int r);
int partition( int p, int r);
// Randomized quick sort
void random_sort(int p, int r);
int random_partition(int p, int r);
void show();
void exch(Type * a, Type * b);
};
Since these two classes are very similar, I want to merge them together. Should I used the abstract based class and alter these two classes since they both require to store the array "A" and its length. Also they share some functions.
So I have this abstract base class:
Code:
template <class Type>
class SortABC
{
private:
Type *A;
int length;
public:
Sort();
virtual ~Sort() = 0;
virtual void show() = 0;
}
I include show() in the ABC because both derived classes will have the same function for show(). Should I include the "exch()" function as well since its present in other two classes?
This means I need another class that inherits from both heap-sort and quick-sort classes. Is this the situation that container would not work and I have to use multiple inheritance? So if I have like 5 or 6 different sorting algorithms, I need to inherit all of them?