I have a txt file of employees with their names, skills and years of experience with that skill. I'm storing their names in ascending order as they are read from the file in an array struct, while their skills and years are stored in a linked list.
This is my attempt, but the logic seems to be off
1 2 3 4 5 6 7 8 9
void insertInOrder (Emp e[], int i, string str) {
int j;
j=i;
while (j>=0 && str[0]<e[j].fname[0]) {
e[j+1] = e[j];
j--;
}
e[j+1].fname = str;
}
I think you have the right idea:
find it,
make a gap by moving everything 1 slot
put it into the gap.
I don't have enough here to debug. Ill try to spot a bug but I don't see yet
alternately, there are a couple of sorts that approach O(N) as the data approaches already sorted, such that adding to the end and sorting is O(N) (its a little more than N iterations, but not by much).