用选择法或冒泡法对输入的50个字符(按照ASII码由小到大)进行排序

#include <stdio.h>//包含标准输入输出函数
#include <stdlib.h>//包含malloc动态地址分配
#include <string.h>//包含memset函数meset(a,0,sizeof(a));//strlen(str);求字符串的长度
/*
用选择法或冒泡法对输入的50个字符(按照ASII码由小到大)进行排序

*/
int main()
{
	void sort(int length,char s[]);//声明排序函数
	char str[55];//使用字符数组存储
	int len;
	scanf("%s",str);
	len=strlen(str);//计算字符数组的长度
	printf("字符串长度%d\n",len);
	sort(len,str);
	return 0;
}
void sort(int lenth,char s[]) //冒泡排序
{
	int i,j;
	char temp;
	for(i=0;i<lenth-1;i++){//len-1次冒泡排序
		for(j=lenth-1;j>i;j--){
			if(s[j-1]>s[j]){//大的在前 逆序
				temp = s[j];
				s[j] = s[j-1];
				s[j-1] = temp;//交换
			}
		}
	}
	for(i=0;i<lenth;i++){
		printf("%2c",s[i]);
	}
	printf("\n");
}
发布了128 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/yky__xukai/article/details/102596718
今日推荐