External merge sort in C++

External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted does not fit into a computer device’s primary memory and, instead, it must reside in the lighter external memory. Typically, external sorting developed a hybrid merging tactic. Amounts of information small enough to fit into primary memory are read, sorted, and written to a temporary file during the sorting process. The sorted sub-files are merged into a single bigger file during the merge phase.


  • Read the input file, and that at many other components of ‘run size’ is the view at a time.
  • Next, read inside an array per each run.
  • Order the execution using Merge Sort.
  • Store the array sorted into a file. Let’s say ‘i’ for file i.
  • Use the approach discussed to merge k sorted arrays to fuse the sorted files


Complexity Analysis:

Time Complexity: O(n + run_size log run_size).

The time needed by the merge sort is O(nlogn). However, there are components in the most run size. Thus the time complexity is O(run size log run size), but then the time complexity is O(n) to merge the sorted arrays. The multiply-accumulate of the time also is O(n + run size log run size).

Auxiliary space:O(run_size).

The run size was its space required for array storage.

One such code will not work on compiler online, as it needs permissions to create files. So, if running a local machine, the sample input file “input.txt” is produced with different characters of 10000. It sorts the number and put the numbers sorted in an “output.txt” file. It also produces files with names 1, 2, .. Sorted passes to shop.

Pin It on Pinterest

Share This