Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort a file that has a very long list of items?

Tags:

c++

sorting

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

like image 412
malhobayyeb Avatar asked Jun 16 '10 11:06

malhobayyeb


2 Answers

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.

like image 128
Jason S Avatar answered Sep 19 '22 11:09

Jason S


Maybe the STXXL (Standard Template Library for Extra Large Data Sets) helps.

STXXL offers external sorting amongst others.

like image 29
stephan Avatar answered Sep 22 '22 11:09

stephan