九度OJ 1066 字符串排序

时间:2023-01-04 18:53:32

题目地址:http://ac.jobdu.com/problem.php?pid=1066

题目描述:

 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出排序后的结果

输入:

 一个字符串,其长度n<=20

输出:

 输入样例可能有多组,对于每组测试样例,

按照ASCII码的大小对输入的字符串从小到大进行排序,输出排序后的结果
样例输入:
dcba
样例输出:
abcd
/*
* Main.c
*
* Created on: 2014年1月25日
* Author: Shaobo
*/
#include <stdio.h>
#include <string.h>

int Partition(char data[], int from, int to){
char pivot = data[from];
while (from < to){
while (from < to && data[to] >= pivot) --to;
data[from] = data[to];
while (from < to && data[from] <= pivot) ++from;
data[to] = data[from];
}
data[from] = pivot;
return from;
}

void QuickSort(char data[], int from, int to){
int pivotpos = Partition (data, from, to);
if (from < pivotpos-1)
QuickSort(data, from, pivotpos-1);
if (to > pivotpos)
QuickSort(data, pivotpos+1, to);
}

int main(void){
char input[21];
int len;

while (scanf ("%s", input) != EOF){
len = strlen(input);
QuickSort(input, 0, len-1);
printf ("%s\n", input);
while (getchar() != '\n')
continue;
}
return 0;
}