idailylife
idailylife

Reputation: 184

How to sort a big file of sentences with variable length?

Suppose I have a file of many lines of string, how to rank the lines of string in lexicographic order?

Details:

What I can figure out is an external merge sort, is there any better idea for this specified problem?

Upvotes: 1

Views: 105

Answers (1)

libik
libik

Reputation: 23049

The difference between file size and the memory is not that big, therefore I would suggest to split file into more smaller files based on first letter - or if it is not enough, by the first two letters.

Then you can sort each of them with quicksort and save it and when then they are sorted, you can put them back together.

It would be still O(N) I/O operations and O(n*log(N)) CPU operations.

PS : External merge sort is also good way.

Upvotes: 2

Related Questions