there is an approach using strcmp() and strcpy() but it is O(n^2) but is there any faster method?
When dealing with arrays if strings, keep in mind that there are two separate quantities that will inform the runtime: how many strings you have (let’s call that n), and how many total characters those strings have (let’s call that m). If either of these values are large, it will increase the runtime. For example, sorting an array of two identical strings that are each a million characters long is going to take a while even though there’s only two strings.
If you have a fixed-sized alphabet (say, just English letters and digits), then you can sort an array of strings in time O(m + n) by inserting all of the strings into a trie and then doing a DFS over the trie to read them back in sorted order. You can achieve a similar effect by using a most-significant character radix sort. These approaches are a bit more involved than simple sorting algorithms combine with strcmp
, but are much faster for large numbers of long strings.
Hope this helps!