Sorting strings by binary strings with arbitrary length

i googled around and talk a lot about sorting radix in a binary string, but are they all the same length, like aobut binary string with arbitrary length?

Say I have {"001", "10101", "011010", "10", "111"}, how can I sort them? Thank!

+3
source share
3 answers

Find the maximum length and overlay them on this length. Should still perform well when there is an upper bound on the length of the longest string.

+2
source

, , , 5 2. , , , . , , , , .

, , - ( → ). , nlogn . radix O (nk) , n - , k - . k, O (nk) O (nlogn) .

+2

, .

Compare that the line length will be without leading 0 (i.e. find firstIndexOf("1")); longer line is longer.
If both are the same length, just keep comparing them by character until you find two characters that are different - the line with "1" is larger.

-1
source

Source: https://habr.com/ru/post/1730717/


All Articles