Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While performing sorting algorithm like insertion sort on a single linked list , is it better to perform it using changing of pointers or just overwriting the data ?

What I have tried:

I have made code for both of them .
Posted
Updated 17-Feb-18 5:28am
v2

Depends.
Generally, swapping pointers will be more efficient, as they are both normally shorter than the data they refer to, and are normally a "machine word" in size, so hardware such as memory systems, caches, etc. are optimised for that size.

But if you are dealing with trivial data lengths, then copying might make sense. Probably not though, as there is the additional overhead of allocating "spare" memory to copy the data to.
 
Share this answer
 
In addition to Griff's good answer, here is a good article about sorting: Visualization and comparison of sorting algorithms in C#[^]
 
Share this answer
 
Quote:
Is it better to overwrite data or change pointers ?

change pointers !
That is the whole reason of existence of linked lists.
Linked list - Wikipedia[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900