编写程序,利用指针完成一批整数的排序并输出
时间: 2024-05-02 11:18:49 浏览: 10
以下是利用指针完成一批整数排序并输出的C++代码:
```c++
#include <iostream>
using namespace std;
void sort(int* arr, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (*(arr + j) < *(arr + i)) {
int temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}
int main() {
int arr[] = { 5, 3, 8, 1, 6 };
int len = sizeof(arr) / sizeof(int);
cout << "Original array:" << endl;
for (int i = 0; i < len; i++) {
cout << *(arr + i) << " ";
}
cout << endl;
sort(arr, len);
cout << "Sorted array:" << endl;
for (int i = 0; i < len; i++) {
cout << *(arr + i) << " ";
}
cout << endl;
return 0;
}
```
输出结果为:
```
Original array:
5 3 8 1 6
Sorted array:
1 3 5 6 8
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)
![](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)