用重载函数实现一组数的升序
时间: 2023-03-28 09:03:25 浏览: 48
可以使用以下代码实现:
#include <iostream>
using namespace std;
void sort(int arr[], int n) {
for(int i=; i<n-1; i++) {
for(int j=; j<n-i-1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void sort(double arr[], int n) {
for(int i=; i<n-1; i++) {
for(int j=; j<n-i-1; j++) {
if(arr[j] > arr[j+1]) {
double temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr1[] = {5, 2, 8, 4, 1};
double arr2[] = {3.5, 1.2, 6.7, 2.1, 8.9};
int n1 = sizeof(arr1)/sizeof(arr1[]);
int n2 = sizeof(arr2)/sizeof(arr2[]);
sort(arr1, n1);
sort(arr2, n2);
for(int i=; i<n1; i++) {
cout << arr1[i] << " ";
}
cout << endl;
for(int i=; i<n2; i++) {
cout << arr2[i] << " ";
}
cout << endl;
return ;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)