【问题标题】:Bubble sort of structures using pointers in C在 C 中使用指针的冒泡排序结构
【发布时间】:2015-06-21 23:58:21
【问题描述】:

我想使用 C 中的冒泡排序算法和指针对结构数组进行排序。 我有一个汽车结构:

typedef struct{
    char model[30];
    int hp;
    int price;
}cars;

我为 12 个项目分配内存:

cars *pointer = (cars*)malloc(12*sizeof(cars));

并从文件中读取数据:

for (i = 0; i <number ; i++) {
    fscanf(file, "%s %i %i\n", (pointer+i)->model, &(pointer+i)->hp, &(pointer+i)->price);
}

我将指针ptr 传递给bubbleSort 函数:

bubbleSort(pointer, number);

这是我的bubbleSort 函数:

void bubbleSort(cars *x, int size) {
    int i, j;
    for (i=0;i<size-1;i++) {
    int swapped = 0;
    for (j = 0; j < size - 1 - i; j++) {
        if ( (x+i)->hp > (x+j+1)->hp ) {
            cars *temp = (x+j+1);
            x[j+1] = x[j];
            x[j] = *temp;
            swapped = 1;
        }
    }
        if (!swapped) {
        //return;
        }
    }
}

问题是我不知道如何使用指针交换项目。

【问题讨论】:

  • 试试cars *temp = (x+j+1);改成cars temp = x[j+1];..x[j] = temp;
  • 还有if ( (x+i)-&gt;hp &gt; (x+j+1)-&gt;hp ) { --> if ( (x+j)-&gt;hp &gt; (x+j+1)-&gt;hp ) {
  • 永远不需要键入 malloc 的返回值。那是 C++ 的事情。只需使用cars *pointer = malloc(12*sizeof(cars));

标签: c pointers struct bubble-sort


【解决方案1】:

考虑以下排序功能的解决方案:

void bubbleSort(cars *x, int size) 
{
    int i, j;
    for (i = 0; i < size-1; i++) 
    {
        for (j = 0; j < size-1-i; j++) 
        {
            if ( x[j].hp > x[j+1].hp ) 
            {
               cars temp = x[j+1];
               x[j+1] = x[j];
               x[j] = temp;
            }
        }
    }
}

问题出在数据交换部分

【讨论】:

  • 现在,在你指出我的更正之后,我的也是一个正确的解决方案,有点不同!
  • 如果您不只是复制示例,您的解决方案将有所不同。当然,同一个问题有无数种不同的解决方案ю 祝你好运!
  • @VolAnd,这可能被认为是标准编码!我不认为 Dogbert 说什么都没有被发现,如果你提到我,请在回复时看到并注意我在 Dogbert 之前写了回复,但我已经纠正了一些错误!
【解决方案2】:
void bubbleSort(cars *x, int size)
{
        int i, j;
        cars temp;

        for (i=0;i<size-1;i++) {
            for (j = i+1; j < size; j++) {
                if ( (x+i)->hp > (x+j)->hp ) {
                    temp = x[j];
                    x[j] = x[i];
                    x[i] = temp;
                }
            }
        }
}

这是对该代码下评论的回复;它表明我建议的那种交换更少...... :) 这里是代码:

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

typedef struct {
    int x;
    int hp;
} cars;

int swaps;

void bubbleSortB(cars *x, int size)
{
        int i, j;
        cars temp;

        for (i=0;i<size-1;i++) {
            for (j = i+1; j < size; j++) {
                if ( (x+i)->hp > (x+j)->hp ) {
                    temp = x[j];
                    x[j] = x[i];
                    x[i] = temp;
                    swaps++;
                }
            }
        }
}

void bubbleSortA(cars *x, int size)
{
    int i, j;
    for (i = 0; i < size-1; i++)
    {
        for (j = 0; j < size-1-i; j++)
        {
            if ( x[j].hp > x[j+1].hp )
            {
               cars temp = x[j+1];
               x[j+1] = x[j];
               x[j] = temp;
               swaps++;
            }
        }
    }
}

int main(void)
{
    int i;

    cars x[10]={ {1,4},{1,8},{1,12},{1,6},{1,5},{1,4},{1,8},{1,12},{1,6},{1,5} };
    cars y[10]={ {1,4},{1,8},{1,12},{1,6},{1,5},{1,4},{1,8},{1,12},{1,6},{1,5} };

    swaps=0;
    bubbleSortA(x,10);
    for(i=0;i<10;i++)
        printf("%d ",x[i].hp);
    printf("- swaps %d\n",swaps);

    swaps=0;
    bubbleSortB(y,10);  //My sort
    for(i=0;i<10;i++)
        printf("%d ",y[i].hp);
    printf("- swaps %d\n",swaps);

}

【讨论】:

  • 你没有取消引用(x+j),而是将一个指向结构的指针分配给一个结构。
  • ...今天我喝醉了... :) 现在一切都应该是正确的!
  • 这不是对“相邻项目对”进行操作的常用冒泡排序算法。它更类似于带有很多无用交换的选择排序。
  • 也许吧。仍然不是冒泡排序。如果性能很重要,那么冒泡排序和选择排序都被淘汰了。
  • 我总是使用库中的 qsort !!!但我的是冒泡排序的优化!
【解决方案3】:

使用这样的交换函数:

#define TYPE <your type>
void swap(TYPE *a, TYPE *b){
        TYPE *temp = (TYPE*)malloc(sizeof(TYPE));
        *temp = *a;
        *a = *b;
        *b = *temp;
        free(temp);
}

或者这个,没有malloc:

void swap(TYPE *a, TYPE *b){
        TYPE temp;
        temp = *a;
        *a = *b;
        *b = temp;
}

【讨论】:

  • 不不不。如果值相同,则将数据清零。这不是一个好技巧。此外,它不会像您认为的结构那样工作。 programmers.stackexchange.com/questions/182037/…
  • 要交换的值不是整数,而是结构体! :)
  • @Dogbert 进行排序,如果它们相同,为什么要交换值?这似乎是教条,但无论如何我都会编辑答案......
  • 此解决方案仅适用于 char、int、long 等整数类型,但不适用于 STRUCTURES!!!
  • 也许是这样,但使用单独的函数使其更具可读性!不过,这只是我个人的喜好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 2012-02-10
相关资源
最近更新 更多