【问题标题】:sorting by two different values按两个不同的值排序
【发布时间】:2016-01-15 16:17:17
【问题描述】:

如果我有一个结构数组,其中包含两个要排序的字段,我该如何实现类似的东西?我已经写了一个修改过的冒泡排序算法来对数组进行排序。

它们应按升序排列,使较小的值在左侧。它们都将是整数值,我只是使用一些字母希望使其更清晰。

假设是一个结构

typedef struct {
    int top;
    int left;
    int right;
    int bottom;
}box;

box** boxes = calloc(1, sizeof(box) * 20);

假设所有这些框的每个位置都有从 -100 到 100 的随机值。

我可以按任何一个角对其进行排序。假设我们有这样的顶部和左侧的盒子,我将使用字母作为左侧,但有它以便数组最终排列成这样。

start top, 0,1,2,3,4,5
start left a,b,c,d,e,f

在开始这样的事情之后:

start top, 5,3,1,2,4,0
start left f,d,a,b,e,c

数组的布局如下:

0,1,2,3,4,5,a,b,c,d,e,f

这是我目前使用的排序算法:

static void obj_swap_func(box** a, box** b) {
    box* tp = *a;
    *a = *b;
    *b = tp;
}

void back_2_front(box** a, int count) {
    cg_sprite* l, *r;

    for (int i = 0; i < count; i++) {
        int swap = 0;
        for (int j = 0; j < (count - i - 1); j++) {
            if (a[j]->l_value > a[j + 1]->l_value) {
                obj_swap_func(&a[j], &a[j + 1]);
                swap++;
            }
        }
        if (swap == 0) {
            break;
        }
    }
}

我写了这个函数,试图实现下面的答案。

static int greater(box* a, box* b) {
    if(a->top < b->top) {
        return 0;
    }
    else if (a->top > b->top) {
        return 1;
    }
    if(a->left < b->left) {
        return 0;
    }
    else if(a->left > b->left) {
        return 1;
    }
    return -1;
}

然后像这样将它添加到循环中: void back_2_front(box** a, int count) { cg_sprite* l, *r;

for (int i = 0; i < count; i++) {
    int swap = 0;
    for (int j = 0; j < (count - i - 1); j++) {
        if(greater(a[j], a[j+1])) {
            box_swap_func(&a[j], &a[j + 1]);
            swap++;
        }
    }
    if (swap == 0) {
        break;
    }
}

}

交换函数如下所示:

static void box_swap_func(box** a, box** b) {
    box* tp = *a;
    *a = *b;
    *b = tp;
}

在一组 50 个盒子上运行,结果如下:

box top:-48 box left: 3
box top:-47 box left: 3
box top:-46 box left: 3
box top:-45 box left: 3
box top:-44 box left: 3
box top:-43 box left: 3
box top:-42 box left: 3
box top:-41 box left: 2
box top:-40 box left: 2
box top:-39 box left: 2
box top:-38 box left: 2
box top:-37 box left: 2
box top:-36 box left: 2
box top:-35 box left: 2
box top:-34 box left: 1
box top:-33 box left: 1
box top:-32 box left: 1
box top:-31 box left: 1
box top:-30 box left: 1
box top:-29 box left: 1
box top:-28 box left: 1
box top:-27 box left: 4
box top:-26 box left: 4
box top:-25 box left: 4
box top:-24 box left: 4
box top:-23 box left: 4
box top:-22 box left: 4
box top:-21 box left: 4
box top:-20 box left: 3
box top:-19 box left: 3
box top:-18 box left: 3
box top:-17 box left: 3
box top:-16 box left: 3
box top:-15 box left: 3
box top:-14 box left: 3
box top:-13 box left: 2
box top:-12 box left: 2
box top:-11 box left: 2
box top:-10 box left: 2
box top:-9 box left: 2
box top:-8 box left: 2
box top:-7 box left: 2
box top:-6 box left: 1
box top:-5 box left: 1
box top:-4 box left: 1
box top:-3 box left: 1
box top:-2 box left: 1
box top:-1 box left: 1
box top:0 box left: 1
box top:2 box left: 4

它按顶部排序很好,但您可以看到它们按顶部值正确排序,而不是按左侧值。

我的算法写对了吗?

【问题讨论】:

  • 你需要说出 box1 的规则是什么(在代码中)> box2
  • 再看一下输出:这些框是按升序排列的,y 坐标比 x 坐标更重要。您在 left 中没有看到顺序是由于缺少共享相同顶部的框。

标签: c arrays algorithm sorting


【解决方案1】:

你必须做的关键修改是替换行

if (a[j]->l_value > a[j + 1]->l_value) {

通过

if ( is_greater(a[j], a[j+1]) ) {

在哪里

int is_greater(box a1, box a2)
{
   // Make the logic as simple or as complex as needed.
}

最简单的实现,相当于你所拥有的,是:

int is_greater(object* a1, object* a2)
{
   return (a1->l_value > a2->l_value);
}

从您的帖子中不清楚boxobject 是如何相关的。如果没有这些信息,我无法提出任何进一步的改进建议。

更新,以回应 OP 的评论

您对is_greater 的实现有问题。

如果你的排序算法可以使用返回值:

小于零

大于零

而不是

-1
0
1

你可以使用:

static int greater(box* a, box* b) {

    if(a->top != b->top) {
       return (a->top - b->top);
    }

    return (a->left - b->left);
}

如果您的排序算法需要 -1、0 和 1,您可以使用:

static int greater(box* a, box* b) {

    if(a->top != b->top) {
       if(a->top < b->top) {
          return -1;
       else
          return 1;
    }

    if (a->left < b->left) {
       return -1;
    }
    else if (a->left > b->left) {
       return 1;
    }

    return 0;
}

【讨论】:

  • 我试过这个,但它似乎只按元素而不是两个排序。我用输出更新了问题。
【解决方案2】:

看起来back_2_front 函数对object 结构的数组进行排序,其中每个实例都有一个l_value 字段。

box 实例而不是object 实例进行排序的最简单方法是定义一个为您进行比较的函数:

int compareBoxes(box * a, box* b)
{
    if(a==NULL or b==NULL) return 0; //do your preferred method of error handling

    //check by top first
    if(a->top < b->top) 
    {
        return 0; //returns false if first object has smaller top
    }
    else if (a->top > b->top)
    {
        return 1; //returns true if first object has greater top
    } 

    //at this point the tops are equal, feel free to compare by other fields...

}

然后在你的内部 for 循环而不是 a[j]-&gt;l_value &gt; a[j + 1]-&gt;l_value 你可以有 compareBoxes(a[j], a[j+1])

请记住,您需要为盒子而不是对象编写交换函数。

【讨论】:

  • 我尝试实现这一点,但我得到的只是对象按第一个元素排序,而第二个元素没有排序。我会用我写的内容更新问题。
  • 你是对的,答案就在那里。谢谢。
猜你喜欢
  • 2020-11-18
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多