I have a working insertion sort algorithm that sorts integers stored in an array. In a different program I have created a struct with words and a count. I need to sort structs stored in an array alphabetically using the same insertion sort. I understand how to compare them, however I cannot find a way to swap them. Ideas?
typedef struct { char * word; int count; } wordType;
You can swap struct
s the same way that you swap integers:
wordType tmp;
wordType a = {.word="hello", .count=5};
wordType b = {.word="world", .count=11};
tmp = a;
a = b;
b = tmp;
Demo on ideone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With