Lexicographic sorting of given set of keys

Lexicographic sorting: Given a set of strings, print them in Lexicographic order (dictionary/alphabetical order).


 

Lexicographic sorting of a set of keys can be accomplished with a simple trie-based algorithm as follows –

  • Insert all keys in a trie.
  • Print all keys in the trie by performing pre-order traversal on trie to get output in lexicographically increasing order.

 
Below is C++ and Java implementation of the idea:

C++

Download   Run Code

Java

Download   Run Code

Output:

a
accomplished
algorithm
all
based
be
by
can
depth
first
in
increasing
insert
is
keys
kind
lexicographic
lexicographically
means
of
order
output
preorder
results
set
simple
sorting
that
the
traversal
trie
we
which
with

 
Thanks for reading.

Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
Abhsihyam
Guest

Java implementation for the above problem

Git Link: https://github.com/abhishyam21/trees/tree/master/src/com/trie

Simran Singh grewal
Guest

What is Its time complexity?

Johnny
Guest

I love it how you guys just used Output list for SEO purposes. :’D

prateek
Guest

We can not store key as leaf . It would dominant trie properties .