Sort Linked List using Insertion Sort

Hello sir/ma’am, I tried to write the code for this problem without using any extra space but on running it the following error is coming----“runguard: warning: timelimit exceeded (wall time): aborting command
runguard: warning: command terminated with signal 15” Could you please help me figure out what’s wrong in my code. Sharing my code: https://ide.codingblocks.com/s/170821

hi @priyanshi.agarwal3405 i think you have some misunderstanding. From what I can see, you are choosing the smallest element in the list and inserting it in the list, am I right? But in insertion sort, you have to iterate through the list and insert each element you come across at the correct place
for eg for 5 4 3 2 1
starting with the second element, 4
we insert it at the correct place, before 5, making the list 4 5 3 2 1, then you go to the next element, 3, and insert it at the correct place making the list 3 4 5 2 1, and you do so until you reach the end of the list
so you are going to need 2 pointers, one to iterate through the list and one to modify the list for each iteration.

Ma’am I modified my logic , my new code is giving segmentation fault, could you please help me debug it…https://ide.codingblocks.com/s/170821

Also please tell if the logic i used this time is correct or not?

@priyanshi.agarwal3405 https://ide.codingblocks.com/s/222126 you can use this for reference

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Ma’am, please tell me whether the approach i used in my code is correct or not? I placed one element from unsorted half into sorted half. That’s what we actually do in insertion sort…right?

Hey @priyanshi.agarwal3405
While the logic you’re thinking of is correct
It is way too complex to practically implement and that is why there are bugs in your implementation
Playing with pointers is anyway difficult and your complex approach may have made it a little more difficult
So for now I request you to see the code my fellow TA sent for reference
After you are done with linked lists then you can try your approach again and be able to solve it more effectively
Best of luck !
If your doubt is resolved please close it

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.