zoukankan      html  css  js  c++  java
  • 冒泡排序

    题目:将二维数组中的字符串排序,*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
    ", characters[i]);
    37     }
    38 
    39     return 0;
    40 }

    结果如下:

  • 相关阅读:
    最大流之dinic
    HDU 2485
    最小费用最大流
    HDU 1533
    HDU 1402
    HDU 1498
    HDU 1281
    Codeforces 283E Cow Tennis Tournament 线段树 (看题解)
    Codeforces 983E NN country 思维 (看题解)
    Codeforces 494D Birthday 树形dp (看题解)
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/6661920.html
Copyright © 2011-2022 走看看