Sort.h #pragma once template class CSort { public: CSort(void); ~CSort(void); void selectionSort(TYPE [], int); void insertionSort(TYPE [], int); void bubbleSort(TYPE [], int); void shellSort(TYPE [], int); void quickSort(TYPE [], int); void heapSort(TYPE [], int); int getComparisions() const { return m_comparisions; } int getMovements() const { return m_movements; } void display() const; private: int m_comparisions; int m_movements; string m_methodName; int m_dataSize; int m_quickSortMinSize; void rquickSort(TYPE [], int, int, int&, int&); void medianLeft(TYPE [], int, int, int&, int&); void quickInsertionSort(TYPE [], int, int, int&, int&); void shiftDown(TYPE [], int, int, int&, int&); }; Sort.cpp #include "StdAfx.h" #include "Sort.h" // för instansiering av templaten template CSort; template CSort; template CSort; template CSort; template CSort::CSort(void) { m_comparisions=m_movements=0; m_quickSortMinSize=10; } template CSort::~CSort(void) { } /////////////////////////////////// // SELECTION SORT // T(n) = A*n^2 + B*n /////////////////////////////////// template void CSort::selectionSort(TYPE v[], int size) { m_comparisions=m_movements=0; m_methodName="Selection sort"; m_dataSize=size; for (int i=0; iv[j]) //ascending order for descending reverse { minat=j; //the position of the min element min=v[j]; m_movements++; } } temp=v[i] ; v[i]=v[minat]; //swap v[minat]=temp; m_movements+=3; }*/ } /////////////////////////////////// // INSERTION SORT // T(n) = A*n^2 + B*n /////////////////////////////////// template void CSort::insertionSort(TYPE v[], int size) { m_comparisions=m_movements=0; m_methodName="Insertion sort"; m_dataSize=size; for (int j=1; j=0) && (++m_comparisions, tmp < v[i]) ) { v[i+1] = v[i]; m_movements++; i--; } v[i+1] = tmp; m_movements++; } } /////////////////////////////////// // BUBBLE SORT // T(n) = n^2 /////////////////////////////////// template void CSort::bubbleSort(TYPE v[],int size) { m_comparisions=m_movements=0; m_methodName="Bubble sort"; m_dataSize=size; for (int i=0; i v[j+1]) { swap(v[j], v[j+1]); swapped=true; m_movements+=3; } } if (!swapped) break; } } /////////////////////////////////// // SHELL SORT // T(n) = A*n^1.25 /////////////////////////////////// template void CSort::shellSort(TYPE v[], int size) { m_comparisions=m_movements=0; m_methodName="Shell sort"; m_dataSize=size; int incre=size/2; while (incre != 0) { for (int current=incre; current=0 && temp void CSort::quickSort(TYPE v[], int size) { m_comparisions=0; m_movements=0; m_methodName="Quick sort"; m_dataSize=size; rquickSort(v, 0, size-1, m_comparisions, m_movements); } template void CSort::heapSort(TYPE v[], int size) { m_comparisions=0; m_movements=0; int i; for (i = (size / 2)-1; i >= 0; i--) shiftDown(v, i, size, m_comparisions,m_movements); for (i = size-1; i >= 1; i--) { swap(v[0],v[i]); m_movements+=3; shiftDown(v, 0, i-1, m_comparisions,m_movements); } } template void CSort::shiftDown(TYPE v[], int first, int last, int& comps, int& moves) { int done, maxChild; done = 0; while ((first*2 <= last) && (!done)) { if (first*2 == last) maxChild = first * 2; else if (comps++,v[first * 2] > v[first * 2 + 1]) maxChild = first * 2; else maxChild = first * 2 + 1; if (comps++,v[first] < v[maxChild]) { swap(v[first],v[maxChild]); m_movements+=3; first = maxChild; } else done = 1; } } template void CSort::rquickSort(TYPE v[], int first, int last, int& comps, int& moves) { if ((last-first) > m_quickSortMinSize) { medianLeft(v,first, last, comps, moves); TYPE pivot=v[first]; moves++; int sortLeft=first+1; int sortRight=last; while (sortLeft <= sortRight) { while (comps++,v[sortLeft]=pivot) sortRight--; if (sortLeft<=sortRight) { TYPE temp=v[sortLeft]; v[sortLeft]=v[sortRight]; v[sortRight]=temp; moves+=3; sortLeft++; sortRight--; } } v[first]=v[sortLeft-1]; v[sortLeft-1]=pivot; moves+=2; if (first < sortRight) rquickSort(v, first, sortRight-1, comps, moves); if (sortLeft void CSort::medianLeft(TYPE v[], int left, int right, int& comps, int& moves) { int mid=(left+right)/2; TYPE temp; if (comps++,v[left]>v[mid]) { temp=v[left]; v[left]=v[mid]; v[mid]=temp; moves+=3; } if (comps++,v[left]>v[right]) { temp=v[left]; v[left]=v[right]; v[right]=temp; moves+=3; } if (comps++,v[mid]>v[right]) { temp=v[mid]; v[mid]=v[right]; v[right]=temp; moves+=3; } temp=v[left]; v[left]=v[mid]; v[mid]=temp; moves+=3; } template void CSort::quickInsertionSort(TYPE v[], int first, int last, int& comps, int& moves) { int i; TYPE temp; int current; for (current=first+1; current<=last; current++) { temp=v[current]; moves++; i=current-1; while (comps++,i>=0 && temp void CSort::display() const { cout << "Method : " << m_methodName << endl; cout << "Data size : " << m_dataSize << endl; cout << "Comparisions : " << m_comparisions << endl; cout << "Movements : " << m_movements << endl; }