若干字符串按字母顺序输出

 1 #include <iostream>
 2 #include <string.h>
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6     void sort(char *name[],int n);
 7     void print(char *name[],int n);
 8     char *name[]={"ball","desk","ruler","cat","dog"};
 9     int n=5;
10     sort(name,n);
11     print(name,n); 
12     return 0;
13 }
14 
15 void sort(char *name[],int n)
16 {
17     char *temp;
18     int i,j,k;
19     for(i=0;i<n-1;i++)
20     {
21         k=i;
22         for(j=i+1;j<n;j++)
23         if(strcmp(name[k],name[j])>0)k=j;
24         if(k!=i)
25         {
26             temp=name[i];
27             name[i]=name[k];
28             name[k]=temp;
29         }
30     }
31 }
32 
33 void print(char *name[],int n)
34     {
35         int i;
36         for(i=0;i<n;i++)
37         cout <<name[i]<<endl;
38     }

 

posted @ 2018-08-01 15:50  borter  阅读(364)  评论(0编辑  收藏  举报