int C[]={11,1,89,7,36,}从小到大排序
时间: 2023-09-19 17:09:11 浏览: 33
好的,这里提供一个简单的冒泡排序算法示例,可以对整型数组进行从小到大排序:
```
void bubbleSort(int arr[], int len) {
int i, j, tmp;
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
```
使用方法:
```
int main() {
int C[] = {11, 1, 89, 7, 36};
int len = sizeof(C) / sizeof(int);
bubbleSort(C, len);
for (int i = 0; i < len; i++) {
printf("%d ", C[i]);
}
return 0;
}
```
这段代码会输出排好序的数组,即 `1 7 11 36 89`。
相关问题
int C[]={11,1,89}从小到大排序
好的,这里提供一个简单的选择排序算法示例,可以对整型数组进行从小到大排序:
```
void selectionSort(int arr[], int len) {
int i, j, minIndex, tmp;
for (i = 0; i < len - 1; i++) {
minIndex = i;
for (j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
}
}
```
使用方法:
```
int main() {
int C[] = {11, 1, 89};
int len = sizeof(C) / sizeof(int);
selectionSort(C, len);
for (int i = 0; i < len; i++) {
printf("%d ", C[i]);
}
return 0;
}
```
这段代码会输出排好序的数组,即 `1 11 89`。
R7-3 从小到大排序pta C语言
以下是一个从小到大排序的示例代码,使用冒泡排序算法:
```c
#include <stdio.h>
int main() {
int nums[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int num_count = sizeof(nums) / sizeof(nums[0]);
int temp;
for (int i = 0; i < num_count - 1; i++) {
for (int j = 0; j < num_count - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
printf("从小到大排序后的结果为:");
for (int i = 0; i < num_count; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}
```
这个代码假设需要排序的数字存储在一个整数数组 `nums` 中,然后使用数组长度计算数组中元素个数,并使用嵌套的 `for` 循环和条件语句实现冒泡排序算法,从而将数组中的数字从小到大排序。排序后,使用 `for` 循环和 `printf()` 函数输出排序后的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)