在C中排序列表的字符串数组

时间:2017-05-28 02:19:26

标签: c arrays string list sorting

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

void ascending_sort(char *name, char **pName, unsigned int size);

int main(){
   char * names[] = {"Ellin","Alexa","Eliza","Celiza", NULL};
ascending_sort(names[0],names,( sizeof(names)/sizeof(char *)) - 1);
return 0;
}

void ascending_sort(char *name, char **pName, unsigned int size){
    unsigned int i;
    unsigned int z;
    int keep;
    char temp;
    /// print out the array
    for (i = 0; i < size; i++)
    {
        printf("str: %s\n", pName[i]);
    }

    ///start sorting...
    for (i = 0; i < size; i++){
        keep = i;
        for(z = i; z < size; z++){
            if(pName[keep]>pName[z]){
               keep = z;
            }
        }
        temp = pName[i];
        pName[i] = pName[keep];
        pName[keep] = temp;

    }
    for (i = 0; i < size; i++){
        printf("str: %s\n", pName[i]);
    }
}

我对双指针(char **)感到困惑。 如何打印出功能中的每个元素元素&#39; for&#39;循环(所以我很容易理解)并对其进行排序(使用&#39; for&#39;循环)?

帮助修复我的排序算法,它没有用

3 个答案:

答案 0 :(得分:0)

char **是指向字符指针的指针。没有索引的数组名称被视为指向数组中第一个元素的指针。你的数组是字符指针(指向其他地方的字符串文字)。因此,指向第一个元素的指针是指向字符指针或char **的指针。如下所示,函数参数也可以指定为char * pnames [],它更好地反映了它是一个字符指针的数组(未知大小)。如果传递大小,for循环最有意义。

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

void ascending_sort(char *name,char *pname[]);    
void ascending_sort2(char *name,char **pnames,unsigned int size);    

int main(){
   char *names[] = {"Dnames", "Bnames", "Anames", "Cnames", NULL};
   ascending_sort(names[0],names);
   ascending_sort2(names[0],names,( sizeof(names)/sizeof(char *)) - 1);
   ascending_sort2(names[0],names,(4));
return 0;
}

void ascending_sort(char *name,char *pname[]){
     //print out list of array
     // calculate array size
     unsigned int i = 0;
     char **ptemp = pname;
     while(*ptemp)  {
    i++;
        ptemp++;
     }
     unsigned int size = i;
     printf("------- 1 ----------\n");
     for (i = 0; i < size; i++)
     {
        printf("str: %s\n", pname[i]);  
     } 
     //start sorting...
}

void ascending_sort2(char *name,char **pnames, unsigned int size){
     //print out list of array
     unsigned int i,z;
     printf("------- 2 ---------\n");
     for (i = 0; i < size; i++)
     {
         printf("str: %s\n", pnames[i]);
     }
     //start bubble sorting...
     for( i = 0; i < (size - 1); i++)
     {
        for ( z = i; z < (size - 1); z++)
        {    
           if ( strcmp(pnames[z],pnames[z+1]) > 0 )
           {
              // swap the pointers
              char * temp = pnames[z+1];
              pnames[z+1] = pnames[z];
              pnames[z] = temp;
           }
        }
     }

     printf("--- after bubble sort: --------\n");
     for (i = 0; i < size; i++)
     {
         printf("str: %s\n", pnames[i]);
     }
}

答案 1 :(得分:0)

使用stdlib中的qsort可能不是你想要的。但是下面的示例代码可能会清除(char **)指针的使用:

files <- list.files(getwd())


for(i in files){
    print(i)
    df <- load(i)
    assign(gsub("\\..*","", i), df ) #extracts the string before period
    rm(df)
    }

答案 2 :(得分:-1)

修正了99.99%...... !!

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

    void sort_name(char **pName, char order[], unsigned int size);

int main()
{ 
    char *names[] = {"Ellin","Alexa","Eliza","4-C", NULL};

    printf("[1] ");
    sort_name(names, "asc", (sizeof(names)/sizeof(char *))-1);

    return 0;
}

void sort_name(char **pName, char order[], unsigned int size){

     unsigned int i;
     unsigned int z;

     ///print out
     printf("Original list:\n\t");
     for (i = 0; i < size; i++)
     {
         (i<size-1) ? printf(" %s,", *(pName+i)) : printf(" %s\n", *(pName+i));
     }

     ///start sorting...
     if (!strcmp(order,"asc")){
            printf("    Ascending sorted list:\n\t");
     for (i=0; i <size; i++){
         for(z=i+1; z<size; z++){
             if(strcmp(*(pName+i),*(pName+z))>0){
                char *temp = *(pName+i);
                *(pName+i) = *(pName+z);
                *(pName+z) = temp;
             }
         }
     }
     for (i = 0; i < size; i++){
         (i<size-1) ? printf(" %s,", *(pName+i)) : printf(" %s\n", *(pName+i));
         }
     }

     else if (!strcmp(order,"desc")){
            printf("    Descending sorted list:\n\t");
     for (i=0; i <size; i++){
         for(z=i+1; z<size; z++){
             if(strcmp(*(pName+i),*(pName+z))<0){
                char *temp = *(pName+i);
                *(pName+i) = *(pName+z);
                *(pName+z) = temp;
             }
         }

     }
     for (i = 0; i < size; i++){
         (i<size-1) ? printf(" %s,", *(pName+i)) : printf(" %s\n", *(pName+i));
         }
     }

     else{
        printf(" That doesnt even make sense!");
     }
}