Sorting a file without loading it into RAM

Write your question here.

If we allow to load file to RAM I can write code myself
but we could have to little amount of RAM to load the file

I think that I can introduce array or list for creating initial runs myself

I would like to use natural merge sort because it is one of the easiest
I have divide function and it seems to work


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

bool splitFile(fstream &fileA, fstream &tempA,fstream &tempB)
{
    string currValue,prevValue;
    bool swapFile,isEmptyB;
    isEmptyB = true;
    if(!fileA.eof())
    {
        getline(fileA,currValue);
        tempA<<currValue<<'\n';
        prevValue = currValue;
    }
    while(!fileA.eof())
    {
        getline(fileA,currValue);
        if(prevValue > currValue)
            swapFile = !swapFile;
        if(swapFile)
            tempA<<currValue<<'\n';
        else
        {
            tempB<<currValue<<'\n';
            isEmptyB = false;
        }
    }
    return isEmptyB;
}

int main()
{
    string pathA,pathTempA,pathTempB;
    fstream fileA,tempA,tempB;
    cout<<"Enter the file you want to split for natural merge sort "<<'\n';
    getline(cin,pathA);
    pathTempA = pathA.substr(0,pathA.find('.'))+"_temp001.txt";
    pathTempB = pathA.substr(0,pathA.find('.'))+"_temp002.txt";
    fileA.open(pathA,ios::in);
    tempA.open(pathTempA,ios::out);
    tempB.open(pathTempB,ios::out);
    if(!fileA.good())
        return 1;
    splitFile(fileA,tempA,tempB);
    fileA.close();
    tempA.close();
    tempB.close();
    return 0;
}



In natural merge sort files that we merge are only partially sorted
and first we need to merge sorted sequences existing in these files
and then merge temporary files

How can i write merge function for natural merge sort

Topic archived. No new replies allowed.