使用C中的Function Comparator进行冒泡排序

时间:2017-04-23 09:05:21

标签: c sorting linked-list comparator bubble-sort

我正在尝试使用函数比较器实现冒泡排序。 我没有收到任何错误,但我没有得到正确的输出。 这是我的比较器功能

int ageComparator( struct student_record_node* node1, struct student_record_node* node2 )
{
  struct student_record_node *ptr1;
  struct student_record_node *lptr;
  ptr1=node1;
  lptr=node2->next_;
  int a=1;

  if (ptr1->record_->student_age_>lptr->record_->student_age_&&ptr1->next_ != NULL)
  {
    //swap(&ptr1, &ptr1->next_);
    return a;
  }
  return 0;
}

这是我的排序功能:

void sort(struct student_record_node **recordsHead, int (*compare_fcn)(struct student_record_node*, struct student_record_node*))
{
int swapped, i;
struct student_record_node *ptr1;
struct student_record_node *lptr = NULL;

do
{
    swapped = 0;
    ptr1 = *recordsHead;

    while (ptr1->next_ != lptr)
    {
        if (compare_fcn(ptr1,ptr1)>0)
        {
            swap(&ptr1, &ptr1->next_);
            swapped = 1;
        }
        ptr1 = ptr1->next_;
    }
    lptr = ptr1;
}
while (swapped);
}

我的交换功能:

void swap(struct student_record_node** node1, struct student_record_node** node2)
{
 student_record_node *tmp;
 student_record_node *n1=NULL;
 student_record_node *n2=NULL;
 n1 = *node1;
 n2 = *node2;
 *tmp->record_= *n1->record_;
 *n1->record_= *n2->record_;
 *n2->record_ = *tmp->record_;

}

我的输出: 在排序之前......

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421758
    student age: 15

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421759
    student age: 19

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421756
    student age: 20

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421758
    student age: 16

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421759
    student age: 11

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421756
    student age: 22
Sorting by age...
struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421759
    student age: 11

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421759
    student age: 11

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421759
    student age: 11

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421759
    student age: 11

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421759
    student age: 11

struct student_record_node:
    student first name: Name
    student second name: Last Name
    student id: 1001421756
    student age: 22

在通过排序功能之后,除了我的姓氏之外的所有值都是相同的。

2 个答案:

答案 0 :(得分:1)

student_record_node *tmp;
...
*tmp->record_= *n1->record_;

tmp未初始化,因此对*tmp的访问是未定义的行为。

答案 1 :(得分:0)

您的交换功能有错误,可以减少。试试这个

想法是交换你的节点所在的地方

void swap(struct student_record_node** node1, struct student_record_node** node2)
{
   student_record_node *tmp;

   tmp = *node2->next;
   *node2->next = *node1->next;
   *node1->next = (tmp) ? tmp->next : NULL; // protection for last element
}
相关问题