简体   繁体   中英

Bubble sort with Function Comparator in C

I am trying to implement Bubble Sort using function comparator. I am not getting any error but I am not getting the right Output. Here is my Comparator Function

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;
}

This is my Sorting Function:

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);
}

My Swapping function:

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_;

}

My Output: Before sorting...

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

After Passing through the sorting function all values except my last is same.

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

tmp is not initialised, therefore access to *tmp is undefined behaviour.

You swap function have error and can be reduced. Try this

The idea is to swap where your node are liked to

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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