I have a text file that has a very long list of items. So I want to sort them alphabetically but I do not want to load all the file into the memory (RAM).
I tried loading all the contents of the file to an array and sort them just like I do normally. But the system complains that there are no much memory!!
Thanks, Mohammad
You'll need to read up on external sorting. The basic approach is to use some sort of divide-and-conquer routine like merge sort, where you read and sort a portion of the file, then read and sort another portion of the file, etc. and when you get to the end you merge the sorted portions together.
Maybe the STXXL (Standard Template Library for Extra Large Data Sets) helps.
STXXL offers external sorting amongst others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With