Return to Snippet

Revision: 68091
at November 26, 2014 06:06 by asdfsdf


Initial Code
#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;
}

Initial URL
http://snippetking.com/View/35/a-modern-approach-to-a-bubble-sort-algorithm-implementation

Initial Description
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.

Initial Title
C++ Bubble Sort Algorithm Using Tempaltes

Initial Tags


Initial Language
C++