繁体   English   中英

选择排序与链表的

[英]Selection sort with linked list's

我有以下数据结构:

struct scoreentry_node {
    struct scoreentry_node *next;
    int score;
    char name[1];    
};  
typedef struct scoreentry_node *score_entry;

我试图创建一个函数,该函数按顺序使用我的结构,并根据名称以升序排列它们。 我想修改输入而不分配任何内存或释放任何东西:

我已经尝试过您的建议:

void selectionsort(score_entry *a) {
    for (; *a != NULL; *a = (*a)->next) {
        score_entry *minafteri = a;
        // find position of minimal element
        for (score_entry j = (*a)->next; j != NULL; j = j->next) {
            if (strcmp(j->name, (*minafteri)->name) == -1) {
                *minafteri = j;
            }
        }
        // swap minimal element to front
        score_entry tmp = *a;
        a = minafteri;
        *minafteri = tmp;
    }
}

我正在使用以下代码测试上面的代码:

score_entry x = add(8, "bob", (add( 8 , "jill", (add (2, "alfred", NULL)))));
iprint("",x);
selectionsort(&x);
iprint("", x);
clear(x); //Frees the whole list

iprint()在结构中打印得分和名称字段。 我的添加函数如下:

score_entry add(int in, char *n, score_entry en) {      
   score_entry r = malloc(sizeof(struct scoreentry_node) + strlen(n));
   r->score = in;
   strcpy(r->name, n);
   r->next = en;  
   return r;   
}

我遇到堆错误,第二张打印不打印排序列表,什么也不打印。 我在做什么错,该怎么解决?

除了按地址传递指针(请参阅下面的注释)之外,您还需要修复交换元素的方式

void selectionsort(score_entry *a) {
  for (; *a != NULL; *a = (*a)->next) 
  {
     score_entry *minafteri = a;
     // find position of minimal element
     for (score_entry j = (*a)->next; j != NULL; j = j->next) {
       if (strcmp(j->name, (*minafteri)->name) == -1) {
         *minafteri = j;
       }
      }
     // swap minimal element to front
     score_entry tmp = *a;
     a = minafteri; // put the minimal node to current position
     tmp->next = (*a)->next ; //fix the links
     (*minafteri)->next=tmp; //fix the links
  }
}

此代码是可怕的! 您不仅没有为我们提供重现问题的所有必要条件(我们无法编译该问题!),而且还隐藏了typedef背后的指针抽象(这也是我们的噩梦)。 一般来说,甚至不应该再使用 C中的链表了,更不用说它们进行排序了。

但是,这里有两个答案。


*minafteri = j; 在您的查找循环中找到的内容实际上会修改您的列表! 为什么您的查找循环应修改列表?

答:不应该! 通过分配minafteri = &j->next ,您将不会使用find循环修改列表...


或者,您可以在该循环内执行交换。

*minafteri = j; 将需要按以下顺序交换以下内容:

  • (*minafteri)->nextj->next
  • *minafterij

您认为单行代码能够执行这两次交换吗? 好吧,它通过了其中的一半...并在此过程中从列表中删除了一堆元素!


以下内容似乎是交换元素的错误尝试:

score_entry *minafteri = a; // half of assigning `a` to `a`
/* SNIP!
 * Nothing assigns to `minafteri`  in this snippet.
 * To assign to `minafteri` write something like `minafteri = fubar;` */
score_entry tmp = *a;       // half of assigning `*a` to `*a`
a = minafteri;              // rest of assigning `a` to `a`
*minafteri = tmp;           // rest of assigning `*a` to `*a`

它真的只是分配*a*aaa ......你认为你需要做的是什么?

我还以为你会发现当你创建你MCVE ......喔,等一下! 你太无耻了!


专注于交换列表中的两个节点作为较小的任务。 完成此操作后,请考虑执行此任务。

您的代码中存在多个问题:

  • if (strcmp(j->name, (*minafteri)->name) == -1) {不正确:当第一个字符串小于第二个字符串时strcmp()不一定返回-1,它可以返回任何负数值。
  • 调整链接以移动较低条目的方式是不正确的:您无法将链接从上一个节点更新为移动到起始节点的链接。 列表已损坏。

这是一个改进的版本:

void selectionsort(score_entry *a) {
    for (; *a != NULL; a = &(*a)->next) {
        // find position of minimal element
        score_entry *least = a;
        for (score_entry *b = &(*a)->next; *b != NULL; b = &(*b)->next) {
            if (strcmp((*b)->name, (*least)->name) < 0) {
                least = b;
            }
        }
        if (least != a) {
            // swap minimal element to front
            score_entry n = *least;
            *least = n->next;   /* unlink node */
            n->next = *a;       /* insert node at start */
            *a = n;
        }
    }
}

这是链表上选择排序的Java实现:

  • 时间复杂度:O(n ^ 2)
  • 空间复杂度:O(1)-选择排序是就地排序算法
class Solution 
{
    public ListNode selectionSortList(ListNode head)
    {
        if(head != null)
        {
            swap(head, findMinimumNode(head));
            selectionSortList(head.next);
        }
        return head;
    }

    private void swap(ListNode x, ListNode y)
    {
        if(x != y)
        {
            int temp = x.val;
            x.val = y.val;
            y.val = temp;    
        }
    }

    private ListNode findMinimumNode(ListNode head)
    {
        if(head.next == null)
            return head;

        ListNode minimumNode = head;

        for(ListNode current = head.next; current != null; current = current.next)
        {
            if(minimumNode.val > current.val)
                minimumNode = current;
        }
        return minimumNode;
    }
}

您必须通过引用将参数传递给selectionsort

void selectionsort(score_entry *a) {
    for (; *a != NULL; *a = (*a)->next) 
    {
      score_entry *minafteri = a;
      // find position of minimal element
      for (score_entry j = (*a)->next; j != NULL; j = j->next) {
      if (strcmp(j->name, (*minafteri)->name) == -1) {
         *minafteri = j;
      }
    }
     // swap minimal element to front
      score_entry tmp = *a;
      a = minafteri;
      *minafteri = tmp;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM