Skip to content

Instantly share code, notes, and snippets.

@darklight721
Last active December 12, 2015 09:39
Show Gist options
  • Save darklight721/4753367 to your computer and use it in GitHub Desktop.
Save darklight721/4753367 to your computer and use it in GitHub Desktop.
Insertion Sort in C using array and linked list. I'm pretty sure my implementation in linked list isn't the most efficient.
void insertion_sort(int list[], int size)
{
int i, j, insertVal;
for (i = 1; i < size; i++) {
insertVal = list[i];
for (j = i; j > 0 && insertVal < list[j - 1]; j--) {
list[j] = list[j - 1];
}
list[j] = insertVal;
}
}
struct node {
int data;
struct node *next;
};
void insertion_sort(struct node **head)
{
struct node *next, *sortedTail, *insertNode;
next = *head ? (*head)->next : NULL;
sortedTail = *head;
while (next) {
insertNode = next;
next = next->next;
insertNode->next = NULL;
if (insertNode->data < sortedTail->data) {
struct node *t1 = *head;
struct node *t2 = NULL;
while (t1 && t1->data < insertNode->data ) {
t2 = t1;
t1 = t1->next;
}
if (t2) {
t2->next = insertNode;
}
else {
*head = insertNode;
}
insertNode->next = t1;
}
else {
sortedTail->next = insertNode;
sortedTail = sortedTail->next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment