int C[]={11,1,89}从小到大排序
时间: 2023-12-17 19:02:26 浏览: 73
好的,这里提供一个简单的选择排序算法示例,可以对整型数组进行从小到大排序:
```
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`。
相关问题
int C[]={11,1,89,7,36,}从小到大排序
好的,这里提供一个简单的冒泡排序算法示例,可以对整型数组进行从小到大排序:
```
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`。
c语言数组排序从小到大排序
在C语言中对数组进行从小到大的排序通常会使用各种算法,如冒泡排序、选择排序、插入排序、快速排序等。这里以简单易懂的冒泡排序为例来讲解:
**冒泡排序**:
```c
#include <stdio.h>
void bubble_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) { // 遍历所有元素
for (int 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 array[] = {5, 2, 8, 1, 9};
int size = sizeof(array) / sizeof(array[0]);
printf("Before sorting: ");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
bubble_sort(array, size);
printf("\nAfter sorting in ascending order: ");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
return 0;
}
```
**相关问题--:**
1. 冒泡排序的时间复杂度是多少?
2. C语言中还有哪些其他的常见排序算法?
3. 如果数组很大,你会选择哪种排序算法?为什么?
阅读全文
相关推荐















