现在的位置: 首页 > 综合 > 正文

C++冒泡排序模板

2012年10月15日 ⁄ 综合 ⁄ 共 327字 ⁄ 字号 评论关闭

 

template<typename T> void Swap(T& a, T& b)  
{  
	T c = a;  
	a = b;  
	b = c;  
}  

template<typename T>
void BubbleSort(T* arr, int n)
{
	for(int i = 0; i < n - 1; i++)
	{
		for(int j = 0; j < n - 1 - i; j++)
		{
			if (arr[j+1] < arr[j])
			{
				Swap(arr[j], arr[j+1]);
			}
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])  
{  
	string a[] = { "asdas", "tdg", "bfds", "dwdad" };  
	BubbleSort(a, 4);
	for(int i = 0; i < 4; i++)  
	{  
		cout<< a[i] << endl;  
	}  
	return 0;  
} 

抱歉!评论已关闭.