冒泡排序

2018-06-18 04:05:25来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

题目:将二维数组中的字符串排序,*characters[] = {"Computer","com.munication","ConsumerElectronics","android","IOS",".text","notepad++","eclipse","code.blocks","VS studio 2015"};

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 char *characters[] = {"Computer","com.munication","ConsumerElectronics","android","IOS",".text","notepad++","eclipse","code.blocks","VS studio 2015"};
 5 
 6 void sort(char *p[], int n)//冒泡排序
 7 {
 8     char *tmp;
 9     int i, j, k;
10     for(i = 0; i < (n - 1); ++i)
11     {
12         k = i;
13         for(j = (i + 1); j < n; ++j)
14         {
15             if(stricmp(p[k], p[j]) > 0)//比较字符串大小的函数stricmp()
16             {
17                 k = j;
18             }
19         }
20         if(k != i)
21         {
22             tmp = p[k];
23             p[k] = p[i];
24             p[i] = tmp;
25         }
26     }
27 }
28 
29 int main()
30 {
31     int n = sizeof(characters)/sizeof(characters[0]);//计算characters的行数
32     sort(characters, n);//传参要统一characters是指向指针的指针
33     int i;
34     for(i = 0; i < n; ++i)
35     {
36         printf("%s\n", characters[i]);
37     }
38 
39     return 0;
40 }

结果如下:

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:统计计算学生成绩类问题汇总

下一篇:1.跨平台开发之~ VSCode开发第一个C程序