Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法编写更好的代码来对C中的结构进行排序?_C_Sorting - Fatal编程技术网

有没有办法编写更好的代码来对C中的结构进行排序?

有没有办法编写更好的代码来对C中的结构进行排序?,c,sorting,C,Sorting,我想把最便宜的放在最上面。c代码运行良好,我只是想知道是否有更好的方法在c中对结构进行排序 typedef struct { float price; char product[50]; char brand[50]; }clothes; void sort(clothes*pt, int count) { int a, b; float price; char product[50]; char brand[50]; for(a = 0; a < co

我想把最便宜的放在最上面。c代码运行良好,我只是想知道是否有更好的方法在c中对结构进行排序

typedef struct
{
  float price;  
  char  product[50]; 
  char  brand[50]; 
}clothes;

void sort(clothes*pt, int count)
{
 int a, b;
 float price;
 char product[50];
 char brand[50];
 for(a = 0; a < count - 1; a++){
    for(b = a + 1; b < count; b++){
        if((pt+a)->price > (pt+b)->price){
            price = ((pt+b)->price);
            strcpy(product,(( pt+b)->product));
            strcpy(brand,((pt+b)->brand));
            ((pt+b)->price) = ((pt+a)->price);
            strcpy(((pt+b)->product),((pt+a)->product));
            strcpy(((pt+b)->brand),((pt+a)->brand));
            ((pt+a)->price) = price;
            strcpy(((pt+a)->product),product);
            strcpy(((pt+a)->brand),brand);
        }
    }
  }
}

您可以,而且可能应该,使用标准排序功能。你需要给它一个比较函数

static int compare_clothes_by_price (const void*p1, const void*p2) 
{
   const clothes* c1 = (const clothes*)p1;
   const clothes* c2 = (const clothes*)p2;
   if (c1->price == c2->price) return 0;
   else if (c1->price < c2-price) return -1;
   else if (c1->price > c2-price) return 1;
   // this is reached only if a price is NAN
   abort();
}

您可以,而且可能应该,使用标准排序功能。你需要给它一个比较函数

static int compare_clothes_by_price (const void*p1, const void*p2) 
{
   const clothes* c1 = (const clothes*)p1;
   const clothes* c2 = (const clothes*)p2;
   if (c1->price == c2->price) return 0;
   else if (c1->price < c2-price) return -1;
   else if (c1->price > c2-price) return 1;
   // this is reached only if a price is NAN
   abort();
}

如果您想提高一点效率:

创建指向结构的指针数组。 稍后,使用qsort对指针数组进行排序。 这与其他答案几乎相同,因此我不再详细解释

我建议的关键区别在于,通过对指向结构的指针进行排序,您不需要每次qsort交换时都复制整个结构的内容

例如,看看其他答案如何通过SizeOffCloth:

 qsort(pt, count, sizeof(clothes), compare);
这意味着当结构很大时,需要做更多的工作 如果你只是对指针进行排序,它会快得多

 qsort(pt, count, sizeof(clothes*), compare);

但是,如果您想提高效率,则需要调整阵列和比较函数

创建指向结构的指针数组。 稍后,使用qsort对指针数组进行排序。 这与其他答案几乎相同,因此我不再详细解释

我建议的关键区别在于,通过对指向结构的指针进行排序,您不需要每次qsort交换时都复制整个结构的内容

例如,看看其他答案如何通过SizeOffCloth:

 qsort(pt, count, sizeof(clothes), compare);
这意味着当结构很大时,需要做更多的工作 如果你只是对指针进行排序,它会快得多

 qsort(pt, count, sizeof(clothes*), compare);
但是您需要调整数组和比较函数

您可以使用标准qsort。下面是一个例子:

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

typedef struct clothes {
    float price;
    char product[50];
    char brand[50];
} clothes;

int sort_clothes(const void *s1, const void *s2) {
    const struct clothes *c1 = s1;
    const struct clothes *c2 = s2;
    if (c1->price == c2->price) {
        return 0;
    } else if (c1->price < c2->price) {
        return -1;
    } else {
        return 1;
    }
}

void print_clothes(struct clothes * c, const size_t count)
{
    for ( size_t i = 0; i < count; ++i ) {
        printf("%s, %s: %f\n", c[i].product, c[i].brand, c[i].price);
    }
}

int main(void)
{
    struct clothes c[] = { {59.99, "Jeans", "Levi"},
                           {10.99, "T-shirt", "Acme"},
                           {5.99, "Socks", "Feet Inc."} };

    printf("Before sorting...\n");
    print_clothes(c, 3);

    printf("\nAfter sorting...\n");
    qsort(c, 3, sizeof *c, sort_clothes);
    print_clothes(c, 3);

    return 0;
}
您可以使用标准qsort。下面是一个例子:

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

typedef struct clothes {
    float price;
    char product[50];
    char brand[50];
} clothes;

int sort_clothes(const void *s1, const void *s2) {
    const struct clothes *c1 = s1;
    const struct clothes *c2 = s2;
    if (c1->price == c2->price) {
        return 0;
    } else if (c1->price < c2->price) {
        return -1;
    } else {
        return 1;
    }
}

void print_clothes(struct clothes * c, const size_t count)
{
    for ( size_t i = 0; i < count; ++i ) {
        printf("%s, %s: %f\n", c[i].product, c[i].brand, c[i].price);
    }
}

int main(void)
{
    struct clothes c[] = { {59.99, "Jeans", "Levi"},
                           {10.99, "T-shirt", "Acme"},
                           {5.99, "Socks", "Feet Inc."} };

    printf("Before sorting...\n");
    print_clothes(c, 3);

    printf("\nAfter sorting...\n");
    qsort(c, 3, sizeof *c, sort_clothes);
    print_clothes(c, 3);

    return 0;
}

标准qsort将是显而易见的方式。更好的方式是更有效的方式?使用快速排序。不如少干活好吗?使用。标准qsort将是显而易见的方法。更好,更有效?使用快速排序。不如少干活好吗?使用