Objective-C 快速枚举冒泡排序
我正在尝试将一些 GCD 集成到我的代码中,并且发现一个严重的瓶颈是我在大型数组中的对象之间执行的气泡比较。这是原始代码:
NSUInteger count = [arrayToDoWorkOn count];
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
[[arrayToDoWorkOn objectAtIndex:i] compare:[arrayToDoWorkOn objectAtIndex:j]];
}
}
明白我的意思了吗?因此,许多其他快速枚举任务可以通过转换为轻松进行 GCD 处理
for (id obj in array)
{
[obj aMessage:stuff];
}
:
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
[obj aMessage:stuff];
}];
有没有办法将我的look-ahead-sorta-bubble-sorta-algorithm-thing 转换为我可以提供给 GCD 块实现的东西?
I'm trying to integrate some GCD into my code, and have found that a severe bottleneck is a bubble comparison I am performing between objects in a large array. Here is the original code:
NSUInteger count = [arrayToDoWorkOn count];
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
[[arrayToDoWorkOn objectAtIndex:i] compare:[arrayToDoWorkOn objectAtIndex:j]];
}
}
Get my drift? So a lot of other fast enumeration tasks can be easily GCD'd by converting
for (id obj in array)
{
[obj aMessage:stuff];
}
to:
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
[obj aMessage:stuff];
}];
Is there a way to convert my look-ahead-sorta-bubble-sorta-algorithm-thing to something that I can feed to a GCD block implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
NSArray
已经有一个内置方法,那么我不建议实现您自己的排序,该方法很可能比您能想出的任何方法都更快地排序。你可以使用这个:现在,如果你需要在排序过程中使用对象,你就会陷入困境,但我建议研究比冒泡排序更有效的排序(快速排序是一个非常好的排序) 。
除此之外,我认为你对 GCD 有点困惑。编写和使用块本质上并不使用 GCD 来执行它;这必须手动完成(严格来说,块只是代码行的集合,本质上与 GCD 没有任何关系;GCD 只是使用块来执行)。
NSArray
的enumerateObjectsUsingBlock:
方法很可能不使用 GCD 来枚举数组(至少参考文献没有对此提供任何见解,所以请证明我错了),并且如果确实如此,这并不是因为你向它提供了一个块,而是因为这就是苹果选择实现它的方式。大多数获取块的方法不使用 GCD 来执行它们。我建议您阅读 Grand Central Dispatch (GCD )参考以及 Cocoa Samurai 的 块和 GCD 指南,以更深入地了解该主题的具体情况。
I wouldn't recommend implementing your own sort if
NSArray
already has a built in method for it that will most likely sort faster than anything you can come up with. You can just use this:Now, if you need to use the objects during the sort, you're in for a pickle, but I'd recommend looking into sorts more efficient than a bubble sort (quick sort is a pretty good one).
Besides this, I think you're a bit confused about GCD. Writing and using a block does not inherently execute it with GCD; that has to be done manually (strictly speaking, a block is simply a collection of lines of code and does not inherently have anything to do with GCD; GCD simply uses blocks for execution).
NSArray
'senumerateObjectsUsingBlock:
method most likely does not use GCD to enumerate the array (at least the reference gives no insight on this, so please prove me wrong), and if it does, it's not because you're supplying it with a block, but rather because that's how Apple chose to implement it. Most methods taking blocks do not use GCD to execute them.I recommend you read the Grand Central Dispatch (GCD) Reference as well as Cocoa Samurai's A Guide to Blocks and GCD to get greater insight into the specifics of the topic.