Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient string sorting algorithm

Sorting strings by comparisons (e.g. standard QuickSort + strcmp-like function) may be a bit slow, especially for long strings sharing a common prefix (the comparison function takes O(s) time, where s is the length of string), thus a standard solution has the complexity of O(s * nlog n). Are there any known faster algorithms?

like image 329
Piotr Turek Avatar asked Aug 07 '11 11:08

Piotr Turek


People also ask

Which sorting algorithm is most efficient to sort string?

Which sorting algorithms is most efficient to sort string consisting of ASCII characters? Explanation: Counting sort algorithm is efficient when range of data to be sorted is fixed. In the above question, the range is from 0 to 255(ASCII range). Counting sort uses an extra constant space proportional to range of data.

Which algorithm is fastest for sorting?

If you've observed, the time complexity of Quicksort is O(n logn) in the best and average case scenarios and O(n^2) in the worst case. But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.

What is efficiency of sorting algorithm?

Sorting algorithms are usually judged by their efficiency. In this case, efficiency refers to the algorithmic efficiency as the size of the input grows large and is generally based on the number of elements to sort. Most of the algorithms in use have an algorithmic efficiency of either O(n^2) or O(n*log(n)).

Which sorting is memory efficient?

The amount of extra memory required by a sorting algorithm is also an important consideration. In place sorting algorithms are the most memory efficient, since they require practically no additional memory. Linked list representations require an additional N words of memory for a list of pointers.


3 Answers

If you know that the string consist only of certain characters (which is almost always the case), you can use a variant of BucketSort or RadixSort.

like image 37
phimuemue Avatar answered Nov 12 '22 14:11

phimuemue


You could build a trie, which should be O(s*n), I believe.

like image 189
Oliver Charlesworth Avatar answered Nov 12 '22 16:11

Oliver Charlesworth


Please search for "Sedgewick Multikey quick sort" (Sedgewick wrote famous algorithms textbooks in C and Java). His algorithm is relatively easy to implement and quite fast. It avoids the problem you are talking above. There is the burst sort algorithm which claims to be faster, but I don't know of any implementation.

There is an article Fast String Sort in C# and F# that describes the algorithm and has a reference to Sedgewick's code as well as to C# code. (disclosure: it's an article and code that I wrote based on Sedgewick's paper).

like image 2
Stefan Savev Avatar answered Nov 12 '22 14:11

Stefan Savev