覆盖数据或更改指针更好吗? [英] Is it better to overwrite data or change pointers ?

查看:105
本文介绍了覆盖数据或更改指针更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单个链表上执行排序算法(如插入排序)时,使用指针更改或仅覆盖数据更好吗?



我尝试过的事情:



我为他们两人制作了代码。

解决方案

< blockquote>取决于。

一般来说,交换指针会更有效率,因为它们通常都比它们所引用的数据短,并且通常是一个机器字大小,所以硬件如内存系统,缓存等都针对该大小进行了优化。



但是如果你正在处理琐碎的数据长度,那么复制可能是有意义的。可能不会,因为分配备用内存来复制数据会产生额外的开销。


除了Griff的好答案之外,这里有一篇关于排序的好文章: C#中排序算法的可视化和比较 [ ^ ]


引用:

覆盖数据或更改指针是否更好?



更改指针!

这就是链接列表存在的全部原因。
链接列表 - 维基百科 [ ^ ]

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 .

解决方案

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.


In addition to Griff's good answer, here is a good article about sorting: Visualization and comparison of sorting algorithms in C#[^]


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[^]


这篇关于覆盖数据或更改指针更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆