void sort(char *str,int n,int flag)
参数说明: arr接收实参数组、n接收数组长度
flag接收升降序,如果为1则完成升序,如果为0则是降序
1 #include<stdio.h>
2 #include<string.h>
3 void Sort(char *str,int n,int flag){
4 int count;
5 char t;
6 if(flag==1){
7 for(int i=1;i<n-1;i++){
8 count=0;
9 for(int j=0;j<n-i;j++){
10 if(*(str+j)>*(str+j+1)){
11 t=*(str+j);*(str+j)=*(str+j+1);*(str+j+1)=t;
12 count++;
13 }
14 }
15 if(count==0)
16 break;
17 }}
18 else{
19 for(int i=1;i<n-1;i++){
20 count=0;
21 for(int j=0;j<n-i;j++){
22 if(*(str+j)<*(str+j+1)){
23 t=*(str+j);*(str+j)=*(str+j+1);*(str+j+1)=t;
24 count++;
25 }
26 }
27 if(count==0)
28 break;
29 }}
30 puts(str);
31 }
32
33 int main(int argc, const char *argv[])
34 {
35 char a[]="dsqasf";
36 int s=strlen(a);
37 int flag;
38 printf("请输入1升序或0降序\n");
39 scanf("%d",&flag);
40 Sort(a,s,flag);
41 return 0;
42 }