请输入您要查询的百科知识:

 

词条 Multi-key quicksort
释义

  1. Description

  2. See also

  3. Notes

  4. References

Multi-key quicksort, also known as three-way radix quicksort,[1] is an algorithm for sorting strings. This hybrid of quicksort and radix sort was originally suggested by P. Shackleton, as reported in one of C.A.R. Hoare's seminal papers on quicksort;[2]{{rp|14}} its modern incarnation was developed by Jon Bentley and Robert Sedgewick in the mid-1990s.[3] The algorithm is designed to exploit the property that in many problems, strings tend to have shared prefixes.

One of the algorithm's uses is the construction of suffix arrays, for which it was one of the fastest algorithms as of 2004.[4]

Description

The three-way radix quicksort algorithm sorts an array of {{mvar|N}} (pointers to) strings in lexicographic order. It is assumed that all strings are of equal length {{mvar|K}}; if the strings are of varying length, they must be padded with extra elements that are less-than any element in the strings.{{Efn|One way to do so without altering the in-memory representation of the strings is to index them using a function that returns −1 or some other small value when the index is out of range.}} The pseudocode for the algorithm is then{{Efn|Arrays and strings are zero-indexed. An array slice {{mono|a[i:j)}} yields the subarray of {{mono|a}} from {{mono|i}} to {{mono|j}} (exclusive) and is assumed to be a non-copying, constant-time operation.}}

 '''algorithm''' sort(a : array of string, d : integer) '''is'''     '''if''' length(a) ≤ 1 '''or''' d ≥ K '''then'''         '''return'''     p := pivot(a, d)     i, j := partition(a, d, p)   ''(Note a simultaneous assignment of two variables.)''     sort(a[0:i), d)     sort(a[i:j), d+1)     sort(a[j:length(a)), d)

The {{mono|pivot}} function must return a single character. Bentley and Sedgewick suggest either picking the median of {{mono|a[0][d], ..., a[length(a)−1][d]}} or some random character in that range.{{r|soda}} The partition function is a variant of the one used in ordinary three-way quicksort: it rearranges {{mono|a}} so that all of {{mono|a[0], ..., a[i−1]}} have an element at position {{mono|d}} that is less than {{mono|p}}, {{mono|a[i], ..., a[j−1]}} have {{mvar|p}} at position {{mvar|d}}, and strings from {{mvar|j}} onward have a {{mono|d}}'th element larger than {{mvar|p}}. (The original partitioning function suggested by Bentley and Sedgewick may be slow in the case of repeated elements; a Dutch national flag partitioning can be used to alleviate this.[5])

Practical implementations of multi-key quicksort can benefit from the same optimizations typically applied to quicksort: median-of-three pivoting, switching to insertion sort for small arrays, etc.[6]

See also

{{Portal|Computer science}}
  • American flag sort{{snd}} another radix sort variant that is fast for string sorting
  • Ternary search tree{{snd}} three-way radix quicksort is isomorphic to this data structure in the same way that quicksort is isomorphic to binary search trees{{r|soda}}

Notes

{{notelist}}

References

1. ^{{DADS|multikey Quicksort|multikeyQuicksort}}
2. ^{{Cite journal | last1 = Hoare | first1 = C. A. R. | authorlink1 = Tony Hoare| doi = 10.1093/comjnl/5.1.10 | title = Quicksort | journal = Comput. J. | volume = 5 | issue = 1 | pages = 10–16 | year = 1962 | pmid = | pmc = }}
3. ^{{cite conference |first1=Jon |last1=Bentley |first2=Robert |last2=Sedgewick |title=Fast algorithms for sorting and searching strings |year=1997 |conference=Proc. Annual ACM-SIAM Symp. on Discrete Algorithms (SODA) |url=http://www.cs.princeton.edu/~rs/strings/paper.pdf |isbn=0-89871-390-0}}
4. ^{{Cite journal| doi = 10.1007/s00453-004-1094-1| title = Engineering a Lightweight Suffix Array Construction Algorithm| journal = Algorithmica| volume = 40| pages = 33–50| year = 2004| last1 = Manzini | first1 = Giovanni| last2 = Ferragina | first2 = Paolo| citeseerx = 10.1.1.385.5959}}
5. ^{{Cite journal | doi = 10.1016/j.ipl.2009.01.007| title = Improving multikey Quicksort for sorting strings with many equal elements| journal = Information Processing Letters| volume = 109| issue = 9| pages = 454–459| year = 2009| last1 = Kim | first1 = Eunsang| last2 = Park | first2 = Kunsoo}}
6. ^{{cite journal |first1=Jon |last1=Bentley |first2=Robert |last2=Sedgewick |title=Sorting Strings with Three-Way Radix Quicksort |year=1998 |journal=Dr. Dobb's Journal |url=http://www.drdobbs.com/database/sorting-strings-with-three-way-radix-qui/184410724}}

3 : Articles with example pseudocode|Comparison sorts|String sorting algorithms

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/21 12:42:31