把数字按照字典形式的进行排序

来源:百度知道 编辑:UC知道 时间:2024/05/23 23:23:56
比如 12,112,123,132,1432,234,233,201
排序后为
112,12,123,132,1432,201,233,234
我需要实现这个功能的C语言的程序 谢谢!

参考例子:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void asort(int a[], int s)
{
int i, j, k, t;
char s1[33], s2[33];

for (i = 0; i < s-1; ++i)
{
k = i;

for (j = i + 1; j < s; ++j)
{
itoa(a[k], s1, 10);
itoa(a[j], s2, 10);

if (strcmp(s1, s2) > 0)
{
k = j;
}
}

if (k != i)
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
}

void main()
{
int a[] = {12,112,123,132,1432,234,233,201};
int i;

asort(a, 8);
for (i = 0; i < 8; ++i)
printf("%d ", a[i]);

printf("\n");
}

没排错啊…