写一个函数实现对输入10个字符按由小到大顺序排列.在主函数中调用该函数并输出排序后的结果

写一个函数实现对输入10个字符按由小到大顺序排列.在主函数中调用该函数并输出排序后的结果。

#include<stdio.h>

void sort(char c[],int n)

{

 int i,j; char t;

 for(i=0;i<n-1;i++)

   for(j=0;j<n-1-i;j++)

     if(c[j]>c[j+1])

     {t=c[j]; c[j]=c[j+1]; c[j+1]=t;}

}

void main()

{

 int i; char c[10];

 printf("Please input 10 chars:\n");

 for(i=0;i<10;i++)

   scanf("%c",&c[i]);

 sort(c,10);

 for(i=0;i<10;i++)

   printf("%c",c[i]);

 printf("\n");

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-08-28
#include<stdio.h>
#define N 10
void fun(char *s,int n) { int i,j; char c;
  for ( i=0;i<n-1;i++ )
    for ( j=i+1;j<n;j++ )
      if ( s[i]>s[j] ) {c=s[i];s[i]=s[j];s[j]=c;}
}
void main() { int i; str[256];
  gets(str); fun(str,N); for ( i=0;i<N;i++ ) printf("%c ",str[i]); printf("\n");
}

第2个回答  2017-08-28
public class QuickSort {
public static void sort(int a[], int low, int hight) {
int i, j, index;
if (low > hight) {
return;
}
i = low;
j = hight;
index = a[i]; // 用子表的第一个记录做基准
while (i < j) { // 从表的两端交替向中间扫描
while (i < j && a[j] >= index)
j--;
if (i < j)
a[i++] = a[j];// 用比基准小的记录替换低位记录
while (i < j && a[i] < index)
i++;
if (i < j) // 用比基准大的记录替换高位记录
a[j--] = a[i];
}
a[i] = index;// 将基准数值替换回 a[i]
sort(a, low, i - 1); // 对低子表进行递归排序
sort(a, i + 1, hight); // 对高子表进行递归排序

}

public static void quickSort(int a[]) {
sort(a, 0, a.length - 1);
}

public static void main(String[] args) {

int a[] = { 49, 38, 65, 97, 76, 13, 27, 49 };
quickSort(a);
System.out.println(Arrays.toString(a));
}
}
//数组里面传多少个数都可以都可以帮你把数排序