c语言字母排序完全正确的一组
时间: 2023-10-28 18:02:50 浏览: 48
以下是完全正确的一组C语言字母排序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 10
void bubbleSort(char arr[]) {
int i, j, temp;
int n = strlen(arr);
for(i=0; i < n-1; i++) {
for(j=0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
// 交换相邻元素
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
char arr[MAX_SIZE] = {'c', 'a', 'f', 'b', 'e', 'd'}; // 待排序数组
printf("排序前的数组:\n");
for(int i=0; i < MAX_SIZE; i++) {
printf("%c ", arr[i]);
}
printf("\n");
bubbleSort(arr); // 调用冒泡排序函数
printf("排序后的数组:\n");
for(int i=0; i < MAX_SIZE; i++) {
printf("%c ", arr[i]);
}
printf("\n");
return 0;
}
```
上面的程序使用了冒泡排序算法对一个包含6个字符的数组进行排序。排序前的数组为{'c', 'a', 'f', 'b', 'e', 'd'},排序后的数组为{'a', 'b', 'c', 'd', 'e', 'f'}。程序首先定义了一个bubbleSort函数,通过比较相邻元素的大小,将较大的元素不断向后交换,从而实现排序。然后在主函数中,先输出排序前的数组,再调用bubbleSort函数进行排序,最后输出排序后的数组。
注意,这只是一个示例程序,它以静态方式定义了一个包含6个字符的数组,并通过预定义的常量MAX_SIZE来限制了数组的最大大小。实际应用中,可以根据具体需求灵活调整数组大小、输入值以及调用排序函数的方式。