#include <iostream>
#include <fstream>
using namespace std;

void simpleMergingSort(const char* name) {
    int a1 = 0, a2 = 0, k = 0, i = 0, j = 0, kol = 0;
    ifstream f(name);
    ofstream f1, f2;


    kol = 0;
    if (!f.is_open()) {
        cout << "Output file can't be read\n";
        return;
    }
    else {
        while (f >> a1) {
            kol++;
        }
        f.close();
    }

    k = 1;
    while (k < kol) {

        f.open(name);
        f1.open("smsort_1");
        f2.open("smsort_2");

        if (!f.is_open()) {
            cout << "Failed to open the main file for reading.\n";
            return;
        }
        if (!f1.is_open() || !f2.is_open()) {
            cout << "Failed to open temporary files for writing.\n";
            return;
        }


        bool toggle = true;
        while (f >> a1) {
            for (i = 0; i < k && (f >> a1 || true); i++) {
                if (toggle) {
                    f1 << a1 << ' ';
                }
                else {
                    f2 << a1 << ' ';
                }
                if (f.eof()) break;
            }
            toggle = !toggle;
        }

        f.close();
        f1.close();
        f2.close();


        ifstream read1("smsort_1");
        ifstream read2("smsort_2");
        ofstream writeMain(name);

        if (!read1.is_open() || !read2.is_open()) {
            cout << "Failed to open temporary files for reading during merge.\n";
            return;
        }
        if (!writeMain.is_open()) {
            cout << "Failed to open the main file for writing during merge.\n";
            return;
        }


        bool has1 = bool(read1 >> a1);
        bool has2 = bool(read2 >> a2);

        while (has1 && has2) {
            if (a1 <= a2) {
                writeMain << a1 << ' ';
                has1 = bool(read1 >> a1);
            }
            else {
                writeMain << a2 << ' ';
                has2 = bool(read2 >> a2);
            }
        }


        while (has1) {
            writeMain << a1 << ' ';
            has1 = bool(read1 >> a1);
        }


        while (has2) {
            writeMain << a2 << ' ';
            has2 = bool(read2 >> a2);
        }

        read1.close();
        read2.close();
        writeMain.close();


        k *= 2;
    }


    remove("smsort_1");
    remove("smsort_2");
}

int main() {
    int N;
    cout << "Type size of array: ";
    cin >> N;
    if (N <= 0) {
        cout << "Size must be positive.\n";
        return 0;
    }
    int* mas = new int[N];
    ofstream file("output.txt");

    for (int i = 0; i < N; i++) {
        cout << "Type " << i << " element of array: ";
        cin >> mas[i];
    }
    for (int i = 0; i < N; i++) {
        file << mas[i] << ' ';
    }
    file.close();


    ifstream file2("output.txt");
    if (file2.fail()) {
        cout << "Error opening file " << endl;
        delete[] mas;
        return 0;
    }
    int a = 0;
    cout << "Array: ";
    while (file2 >> a) {
        cout << a << ' ';
    }
    cout << endl;
    file2.close();


    simpleMergingSort("output.txt");


    ifstream sortedFile("output.txt");
    if (!sortedFile.is_open()) {
        cout << "Error opening sorted file.\n";
        delete[] mas;
        return 0;
    }
    cout << "After sort array: ";
    while (sortedFile >> a) {
        cout << a << ' ';
    }
    cout << endl;
    sortedFile.close();

    delete[] mas;
    return 0;
}

Type size of array: 5
Type 0 element of array: 5
Type 1 element of array: 3
Type 2 element of array: 2
Type 3 element of array: 4
Type 4 element of array: 1
Array: 5 3 2 4 1
After sort array: 4 4