1

The linked list contain two types of data - int count and float rank. The list needs to be sorted in descending order based firstly on count and then on rank. I have sorted the list based on count, but it appears I don't get the process completely.

I understand that if current count is equal to the next count, then compare rank and insert node with higher rank.

I have changed the code after the comment, however it does output data correctly (does not sort by the rank). How can I fix it?

Sorted list by the first parameter (what I currently get):

2, 0.152009
2, 0.077676
2, 0.092575
2, 0.157685
1, 0.262355
1, 0.184311
1, 0.073388

Expected output after sorting list by the second parameter:

2, 0.157685
2, 0.152009
2, 0.092575
2, 0.077676
1, 0.262355
1, 0.184311
1, 0.073388

My code:

typedef struct node {
    int count;
    float rank;
    struct node *next;
} NodeT; 

void insertionSort(NodeT **head) {
    
    NodeT *sorted = NULL;
    NodeT *current = *head;
    while (current != NULL) {
        NodeT *next = current->next;
        sortedInsert(&sorted, current);
        current = next;
    }
    *head = sorted;
}

void sortedInsert(NodeT **head, NodeT *new) {

    NodeT *current;

    if (*head == NULL || (*head)->count <= new->count) {
        new->next = *head;
        *head = new;

    } else {
        current = *head;
        while (current->next != NULL) {

            /* Should second parameter check go here? */

            if (current->next->count > new->count) {
                current = current->next;

            } else if (current->next->count == new->count) {

                while (current->next != NULL && current->next->rank > new->rank) {
                    current = current->next;
                } 
            }

        }
        new->next = current->next;
        current->next = new;
    }
}
4
  • What does "it does not work" mean exactly? Have you tried debugging your code? You'll learn a more by stepping through it in a debugger and inspecting your variables. And do post an minimal reproducible example, please. Commented Jan 15, 2021 at 7:00
  • @jwdonahue I have described the problem with sample of expected output and current output, and stated that I was not able to sort by the second parameter with current code and asked how can I fix it. What other details are you looking for? Commented Jan 15, 2021 at 7:08
  • Read How to Ask, minimal reproducible example and softwareengineering.meta.stackexchange.com/questions/6166/… Commented Jan 15, 2021 at 7:11
  • 1
    Can you write out your intended algorithm in english? I find it helps to describe an algorithm before I try to write it. Commented Jan 15, 2021 at 7:13

2 Answers 2

4

The "correct" way is probably to write a comparison function to avoid the two nested while loops.

int compare_nodes(NodeT *n1, NodeT *n2)
{
    if (n1->count < n2->count)
        return -1;
    if (n1->count > n2->count)
        return 1;
    if (n1->rank < n2->rank)
        return -1;
    if (n1->rank > n2->rank)
        return 1;
    return 0;
}

Now you should have a much easier time implementing the insert function.

Sign up to request clarification or add additional context in comments.

Comments

1

In Is there a way to sort structs by multiple variables in C? is an example that uses qsort with a defined compare function (cmp()). It implements the nodes as struct, in main() the list is created...

An example for insertion sort is in Insertion sort array of structure by 2 fields

1 Comment

Given that this seems to be the OP's homework assignment, and they appear to be attempting to write an insertion sort algorithm, it's probably safe to assume that a qsort would not be an acceptable answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.