字符串排序

来源:互联网 发布:淘宝手机端详情编辑 编辑:程序博客网 时间:2024/06/07 23:55
#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#define N 10 void prt(char *p[]){for (int i=0;i<N;i++){puts(p[i]);}}void sort1(char *p[]){for (int i=0;i<5;i++){for (int j=0;j<N-1;j++){if (strcmp(p[j],p[j+1])>0){char *t = p[j];p[j] = p[j+1];p[j+1] = t;}}}}void sort2(char *p[]){for (int i = 0; i<5; i++){for (int j = 0; j<N - 1; j++){if (strcmp(p[j], p[j + 1])>0){char *t = p[j];p[j] = p[j + 1];p[j + 1] = t;}}}}void main(){char *name[] = {"CHINA","AUSTRALIA","AMERICA","FRANCE","GERMAN","CANADA","ENGLISH","KOREA","JAPAN","AGUMA"};printf("原顺序国家名:\n");prt(name);sort1(name);printf("排序1后国家名:\n");prt(name);sort2(name);printf("排序2后国家名:\n");prt(name);/*char *s1 = "AMERICA";char *s2 = "AUSTRALIA";*/printf("\n");system("pause");}

0 0
原创粉丝点击