冒泡排序

#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H
#include<assert.h>
template<class T,int n>
inline void swap(T* s,int i,int j)
{
	assert((i<n)&&(j<n));
	T temp=s[i];
	s[i]=s[j];
	s[j]=temp;
}

template<class T,int n>
void bubble_up(T* s, int i)
{
	for(int j=n-1;j-1>=i;j--)
	{
       if(s[j]<s[j-1])
		   swap<T,n>(s,j,j-1);
	}
}

template<class T,int n>
void bubble_sort(T* s)
{
	int i=0;
	for(;i<n-1;i++)
		bubble_up<T,n>(s,i);
}
#endif
posted @ 2011-07-19 19:54  樱色布  阅读(124)  评论(0编辑  收藏  举报