1

I am trying to use insertion sort on a singly linked list, where each node has multiple parameters, say:

typedef struct _node {
    char param1[10];
    char param2[10];
    char param3[10];
    struct _node *next;
} node;

The algorithm would sort the list by param1, and in case of equivalent items decide the order by param2, and similarly by param3. I was unable to find a solution to this problem online. For a single parameter, I've got the following implementation of the sorting algorithm to work:

void insertionSort(node **head) {

    //Initialize sorted list
    node *sorted = NULL;
    node *current = *head;

    //Insert every node into sorted list
    while (current != NULL) {
        // Store next for next iteration
        node *next = current->next;

        sortedInsert(&sorted, current);

        // Update current
        current = next;
    }

    //Replace old empty list with sorted list
    *head = sorted;
}

void sortedInsert(node **head, node *new) {
    node *current = *head;
    // Special case for the head
    if (current == NULL || strcmp(current->param1, new->param1) >= 0) {
        new->next = current;
        *head = new;
    }

    else {
        while (current->next!=NULL && strcmp(current->next->param1, new->param1) < 0){
            current = current->next;
        }

        new->next = current->next;
        current->next = new;
    }
}

My guess would be that I have to create a comparison function instead of the following bits, maybe an integer function returning 0 or 1 corresponding to the two cases:

current->param1 >= new->param1
current->next->param1 < new->param1

However, I am unable to come up with such a function. Could anyone help me out with this?

rob
  • 105
  • 8
  • 2
    A sort routine, makes decisions about what comes before or after by comparison. If the comparison is done in a function, then by the return (e.g. less than zero, zero, greater than zero). If you are sorting on primary parameters and encounter an equivalence, then compare by your secondary string and use those results for the placement. In most cases, the comparison function will take a void pointer to each node being compared, so you will have access to both the primary and secondary strings within the function. Just include a secondary comparison in case the result of the first equals zero. – David C. Rankin Nov 16 '15 at 05:10
  • 1
    FWIW, `compare`-type functions usually return 0 to indicate that the two arguments are equal, a positive integer to indicate that the first argument is greater than the second, and a negative integer to indicate that the first argument is lesser than the second. (For example, this is what `strcmp` does.) I recommend either adopting that convention, or choosing a name such as `less_than` (and returning `bool`, provided by ``). – ruakh Nov 16 '15 at 06:55
  • @DavidC.Rankin could you make your comment an answer so I could accept it? – rob Nov 17 '15 at 02:04
  • Sure, be happy to. Will be done in a minute. – David C. Rankin Nov 17 '15 at 02:50

1 Answers1

0

A sort routine, makes decisions about what comes before or after by comparison. If the comparison is done in a function, then the order of the elements is determined by the return (e.g. less than zero (generally -1), zero, greater than zero (generally a return of 1)).

If you are sorting on primary parameters and encounter an equivalence, then compare by your secondary string and use those results for the placement. In most cases, the comparison function will take a void pointer to each node being compared. Done this way, you will have access to both the primary and secondary strings within the sort function. Just include a secondary comparison in your sort function in case the result of the first comparison equals zero and return (-1, 0, 1) based on the results of that secondary comparison.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85