冒泡排序

#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

你可能感兴趣的:(冒泡排序)