用C++语言并运用指针写出对整数进行排序的代码
时间: 2023-04-02 16:03:11 浏览: 53
以下是使用 C 语言并运用指针写出对整数进行排序的代码:
#include <stdio.h>
void sort(int *arr, int n) {
int i, j, temp;
for (i = ; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (*(arr + i) > *(arr + j)) {
temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}
int main() {
int arr[] = { 5, 3, 8, 4, 2 };
int n = sizeof(arr) / sizeof(int);
int i;
printf("排序前:");
for (i = ; i < n; i++) {
printf("%d ", *(arr + i));
}
sort(arr, n);
printf("\n排序后:");
for (i = ; i < n; i++) {
printf("%d ", *(arr + i));
}
return ;
}
相关问题
用c++写一个给100个整数排序的代码
以下是使用冒泡排序算法的C代码,可以对100个整数进行排序:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[100], n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
```
该程序首先要求用户输入要排序的100个整数,然后使用冒泡排序算法对它们进行排序,并在屏幕上输出排序后的数组。
对十个整数进行排序c++
可以使用C++ STL中的sort函数对十个整数进行排序,代码如下:
```c++
#include <iostream>
#include <algorithm> // 包含sort函数
using namespace std;
int main()
{
int nums[10] = {5, 2, 9, 7, 3, 8, 1, 6, 4, 0}; // 待排序的十个整数
sort(nums, nums+10); // 使用sort函数排序
for(int i=0; i<10; i++)
{
cout << nums[i] << " "; // 输出排序后的结果
}
return 0;
}
```
输出结果为:
```
0 1 2 3 4 5 6 7 8 9
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)