/ Published in: C++
Here I present a modern template based approach to a bubble sorting algorithm. The complexity of this algorithm runs in O(n^2) order of complexity.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include "stdafx.h" /** Core bubble sort algorithm. */ template<class T> void BubbleSort(T* pElements, size_t uNumElements, int (*pComparison)(T const* pA, T const* pB)) { for(int i = 0; i < uNumElements; i++) { for(int j = i+1; j < uNumElements; j++) { int iCompare = pComparison(&pElements[i], &pElements[j]); if(iCompare > 0) { T tSwapElement = pElements[i]; pElements[i] = pElements[j]; pElements[j] = tSwapElement; } } } } /** Comparison function passed to bubble sort. */ int IntCompare(int const* pA, int const* pB) { return *pA - *pB; }
URL: http://snippetking.com/View/35/a-modern-approach-to-a-bubble-sort-algorithm-implementation