排序一个字符串数组 [英] sort an array of strings

查看:78
本文介绍了排序一个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有谁能告诉我一个有效的算法来排序字符串数组?

请记住,这个数组是巨大的,所以算法应该我足够高效地处理它。


谢谢

解决方案
"造雨" < CA ************* @ gmail.com>写道:

有谁能告诉我一个有效的算法来排序一个字符串数组?
请记住,这个数组是巨大的,所以算法应该我足够有效处理用它。




qsort()应该合理有效。通过滚动自己的排序函数(可能基于一个
qsort()实现 - 这不一定是Quicksort),你可能会获得更好的性能。 />
可以避免间接调用

比较函数所造成的开销。


显然,排序指针而不是改变字符串周围的字符串

将为您节省大量时间。 (我甚至不确定如果他们的长度变化很长,你会怎么回事。

关于自己洗牌的事情。)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Rainmaker说:



任何人都可以告诉我是一个有效的算法来排序一个字符串数组?
请记住,这个数组是巨大的,所以算法应该有足够的效率来处理它。




Quicksort或mergesort应该没问题。你选择哪个取决于阵列是多么的巨大。


别忘了你可能不需要对字符串数组进行排序好吧。

你很可能只需要排序一个

指针数组,其中每个指针指向其中一个字符串。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Keith Thompson写道:

Rainmaker < CA ************* @ gmail.com>写道:

有谁能告诉我一个有效的算法来排序一个字符串数组?
请记住,这个数组是巨大的,所以算法应该我
足够有效处理它。



qsort()应该合理有效。你可以通过滚动你自己的排序函数(可能基于一个qsort()实现 - 这不一定是Quicksort)来获得更好的性能,这可以避免由于对
比较函数的间接调用。

显然,排序指针而不是将字符串拖拽
将为您节省大量时间。 (如果他们的长度可变,我甚至不确定你会如何改变自己的字符串。)



你如果它们相邻,可以自动改变可变长度的字符串,

表示插入排序。这很慢。如果可以的话,排序指针是一个很好的想法。


-

Joe Wright

所有事情都应尽可能简单,但并不简单。

---阿尔伯特爱因斯坦---


Hi,

Can anyone tell me an efficient algorithm to sort an array of strings?
Keep in mind that this array is HUGE and so the algorithm should me
efficient enough to deal with it.

Thanks

解决方案

"Rainmaker" <ca*************@gmail.com> writes:

Can anyone tell me an efficient algorithm to sort an array of strings?
Keep in mind that this array is HUGE and so the algorithm should me
efficient enough to deal with it.



qsort() should be reasonably efficient. You might get better
performance by rolling your own sorting function (perhaps based on a
qsort() implementation -- which isn''t necessarily Quicksort), which
could avoid the overhead caused by the indirect calls to the
comparison function.

Obviously, sorting pointers rather than shuffling the strings around
is going to save you a lot of time. (I''m not even sure how you''d go
about shuffling the strings themselves if they''ve of variable
lengths.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Rainmaker said:

Hi,

Can anyone tell me an efficient algorithm to sort an array of strings?
Keep in mind that this array is HUGE and so the algorithm should me
efficient enough to deal with it.



Quicksort or mergesort should do fine. Which you pick depends on just how
huge the array is.

Don''t forget you probably don''t need to sort the array of strings at all.
It''s quite likely you can get away with merely sorting an array of
pointers, where each pointer points to one of the strings.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Keith Thompson wrote:

"Rainmaker" <ca*************@gmail.com> writes:

Can anyone tell me an efficient algorithm to sort an array of strings?
Keep in mind that this array is HUGE and so the algorithm should me
efficient enough to deal with it.


qsort() should be reasonably efficient. You might get better
performance by rolling your own sorting function (perhaps based on a
qsort() implementation -- which isn''t necessarily Quicksort), which
could avoid the overhead caused by the indirect calls to the
comparison function.

Obviously, sorting pointers rather than shuffling the strings around
is going to save you a lot of time. (I''m not even sure how you''d go
about shuffling the strings themselves if they''ve of variable
lengths.)


You can shuffle variable length strings themselves if they''re adjacent,
suggesting a insertion sort. It is very slow. Sorting pointers is a much
better idea if you can.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


这篇关于排序一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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