问题描述
有谁能告诉我一个有效的算法来排序字符串数组?
请记住,这个数组是巨大的,所以算法应该我足够高效地处理它。
谢谢
qsort()应该合理有效。通过滚动自己的排序函数(可能基于一个
qsort()实现 - 这不一定是Quicksort),你可能会获得更好的性能。 />
可以避免间接调用
比较函数所造成的开销。
显然,排序指针而不是改变字符串周围的字符串
将为您节省大量时间。 (我甚至不确定如果他们的长度变化很长,你会怎么回事。
关于自己洗牌的事情。)
-
Keith Thompson(The_Other_Keith)
电子邮件:rjh在上面的域名(但显然放弃了www)
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
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.
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)
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 ---
这篇关于排序一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!