2. 编写一冒泡排序的函数模板,能够对不同类型的数据进行排序
#include<iostream> using namespace std; template<typename T> void bubblesort(T array[],int length) { T temp; int i,j; for(i = 1; i < length ; i++) { for(j = 0; j < length - i ; j++) { if(array[j] > array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } int main() { int n,i; cout << "请输入你要你比较数的个数(int类型):" << endl; cin >> n ; double a[n]; for(i=0;i<n;i++) { cin >> a[i]; } bubblesort(a,n); for(i=0;i<n;i++) { cout << a[i] <<" "; } cout << endl; cout << "请输入你要你比较数的个数(double类型):" << endl; cin >> n ; double b[n]; for(i=0;i<n;i++) { cin >> b[i]; } bubblesort(b,n); for(i=0;i<n;i++) { cout << b[i] <<" "; } cout <<endl; cout << "请输入你要你比较数的个数(char类型):" << endl; cin >> n ; char c[n]; for(i=0;i<n;i++) { cin >> c[i]; } bubblesort(c,n); for(i=0;i<n;i++) { cout << c[i] <<" "; } }