在 C 中对 10 个整数的数组进行冒泡排序的 RAM 消耗的粗略估计是否可能?

发布于 2025-01-07 14:24:36 字数 126 浏览 3 评论 0原文

是否有可能在纸上精确估计在一个简单的数据集(10 个整数数组)上用 C 编写的简单算法(冒泡排序)将消耗多少 RAM?或者编译器实现问题和“字节填充”会让这变得不可能吗?

(给定一个平台,例如 32 位 x86 机器)。

Is it possible to precisely estimate on paper how much RAM will be consumed for a simple algorithm (bubble sort) in C on a trivial dataset (10 integer array)? Or will compiler implementation concerns and 'byte padding' make this impossible?

(Given a platform such as a 32bit x86 machine).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你的星星会说话 2025-01-14 14:24:36

冒泡排序可以就地工作,因此除了您排序的数组之外,它不需要任何内存。

一个 10 整数数组需要 40 个字节,加上一些小的平台相关分配开销。
如果您想要真正精确的估计,则需要考虑可执行文件的大小、用于进程管理的内存等等。但在通常拥有大量内存的 x86 上,这些事情真的没什么好担心的。

如果数组较大,则每个整数需要 4 个字节,并且保持不变的开销可以忽略不计。整数之间没有填充,因此对于大型数组,您应该关心的是每个整数的 4 个字节。

Bubble sort can work in-place, so it needs no memory except the array you sort.

A 10 integer array takes 40 bytes, plus some small platform dependent allocation overhead.
If you want a really precise estimate, you need to consider the size of the executable, memory used for process management, and more. But on x86, which normally has lots of memory, these things are really nothing to worry about.

If the array is larger, then it takes 4 bytes per integer, and the overheads, which stay the same, become negligible. There's no padding between the integers, so for large arrays, all you should care about is the 4 bytes per integer.

任谁 2025-01-14 14:24:36

这听起来像是家庭作业,所以我会问你一些问题:

  1. 排序算法是对它们的项目进行就地排序还是对副本进行排序?
  2. 如果他们处理副本,复制列表的大小不会主导计算吗?
  3. 如果它们就地排序,除了列表空间之外还需要多少存储空间?
  4. (3) 中的额外存储量是否取决于列表的大小?

This sounds like homework so I'll ask some questions of you:

  1. Do sort algorithms sort their items in place or do they work on a copy?
  2. If they work on a copy, wouldn't the size of the copied list dominate the calculation?
  3. If they sort in place, how much storage is needed beyond the space of the list?
  4. Does the amount of additional storage in (3) depend on the size of the list?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文