沈阳市最好的幼儿园:C语言排序 高手进

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/28 03:25:16
问题:
输入 China
Japan
America
按从小到大的顺序输出
字符串排序问题

书上的例题,建议自己多看下书:
方法一:
普通的排序方法, 逐个比较之后交换字符串的位置
void main()
{
char st[20],cs[5][20];
int i,j,p;
printf("input country's name:\n");
for(i=0;i<5;i++)
gets(cs[i]);
printf("\n");
for(i=0;i<5;i++)
{ p=i;strcpy(st,cs[i]);
for(j=i+1;j<5;j++)
if(strcmp(cs[j],st)<0) {p=j;strcpy(st,cs[j]);}
if(p!=i)
{
strcpy(st,cs[i]);
strcpy(cs[i],cs[p]);
strcpy(cs[p],st);
}
puts(cs[i]);}printf("\n");
}
for(i=0;i<5;i++)
{ p=i;strcpy(st,cs[i]);
for(j=i+1;j<5;j++)
if(strcmp(cs[j],st)<0) { p=j;strcpy(st,cs[j]);}
if(p!=i)
{
strcpy(st,cs[i]);
strcpy(cs[i],cs[p]);
strcpy(cs[p],st);
}
方法2:

交换字符串的物理位置是通过字符串复制函数完成的。 反复的交换将使程序执行的速度很慢,同时由于各字符串(国名) 的长度不同,又增加了存储管理的负担。 用指针数组能很好地解决这些问题。把所有的字符串存放在一个数组中, 把这些字符数组的首地址放在一个指针数组中,当需要交换两个字符串时, 只须交换指针数组相应两元素的内容(地址)即可,而不必交换字符串本身。
#include"string.h"
main(){
void sort(char *name[],int n);
void print(char *name[],int n);
static char *name[]={ "CHINA","AMERICA","AUSTRALIA",
"FRANCE","GERMAN"};
int n=5;
sort(name,n);
print(name,n);
}
void sort(char *name[],int n){
char *pt;
int i,j,k;
for(i=0;i<n-1;i++){
k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0) k=j;
if(k!=i){
pt=name[i];
name[i]=name[k];
name[k]=pt;
}
}
}
void print(char *name[],int n){
int i;
for (i=0;i<n;i++) printf("%s\n",name[i]);
}
程序中定义了两个函数,一个名为sort完成排序, 其形参为指
针数组name,即为待排序的各字符串数组的指针。形参n为字符串的个数。另一个函数名为print,用于排序后字符串的输出,其形参与sort的形参相同。主函数main中,定义了指针数组name 并作了初始化赋值。然后分别调用sort函数和print函数完成排序和输出。值得说明的是在sort函数中,对两个字符串比较,采用了strcmp 函数,strcmp函数允许参与比较的串以指针方式出现。name[k]和name[ j]均为指针,因此是合法的。字符串比较后需要交换时, 只交换指针数组元素的值,而不交换具体的字符串, 这样将大大减少时间的开销,提高了运行效率。
这里只是引用的书上的例子,不是做的题目。

用指针数组比较简单.
/***********指针数组批量排列*************/
#include<stdio.h>
#include<string.h>
/****************************************/
void sortr(char *x[],int y);
/****************************************/
int main(void)
{
char *a[]={"Hello","Good","Thank you","Wool","Mir2"};
int i;
sortr(a,5);
for(i=0;i<5;i++)
printf("%s\n",a[i]);
return 0;
}
/****************************************/
void sortr(char *x[],int y)
{
int i,j;
char *p;
for(i=0;i<y-1;i++)
for(j=y-1;j>i;j--)
if(strcmp(x[j-1],x[j])>0)
{
p=x[j];
x[j]=x[j-1];
x[j-1]=p;
}
}
/****************************************/

上面几个字符自己改改吧.

用STL的标准函数

void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );

base是需要sort的数组,num是base的大小,width是每个元素的大小,以byte为单位,compare是一个比较大小的函数的指针,当然是你自己写的了,因为qsort也不知道你要排序的是什么东西啊,你就告诉它是elem1大呢还是elem2大就行了,返回值看看下面的详细文档啦。

The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call:

compare( (void *) elem1, (void *) elem2 );

The routine must compare the elements, then return one of the following values:

Return Value Description
< 0 elem1 less than elem2
0 elem1 equivalent to elem2
> 0 elem1 greater than elem2

The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function.

Example
/* QSORT.C: This program reads the command-line
* parameters and uses qsort to sort them. It
* then displays the sorted arguments.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int compare( const void *arg1, const void *arg2 );

void main( int argc, char **argv )
{
int i;
/* Eliminate argv[0] from sort: */
argv++;
argc--;

/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );

/* Output sorted list: */
for( i = 0; i < argc; ++i )
printf( "%s ", argv[i] );
printf( "\n" );
}

int compare( const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}

Output
[C:\code]qsort every good boy deserves favor
boy deserves every favor good

=我在看看书

不懂,能否详细一点?