. 下面程序的作用是定义一个冒泡排序的函数模板,并在主函数中调用,请按题目要求填空。(6分)
时间: 2023-06-26 15:07:16 浏览: 55
```cpp
#include<iostream>
using namespace std;
template<typename T>
void bubbleSort(T arr[],int len){
for(int i=0;i<len-1;++i){
for(int j=0;j<len-i-1;++j){
if(arr[j]>arr[j+1]){
T temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main(){
int arr1[]={1,3,2,5,4};
double arr2[]={2.5,1.2,3.7,0.8,4.1};
bubbleSort<int>(arr1,5);
bubbleSort<double>(arr2,5);
for(int i=0;i<5;++i){
cout<<arr1[i]<<" ";
}
cout<<endl;
for(int i=0;i<5;++i){
cout<<arr2[i]<<" ";
}
cout<<endl;
return 0;
}
```
填空:
```cpp
template<typename T>
void bubbleSort(T arr[],int len){
for(int i=0;i<len-1;++i){
for(int j=0;j<len-i-1;++j){
if(arr[j]>arr[j+1]){
T temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
```