How can I read the numbers from a file, then write them into two files in ascending order, one at a time, into one file, then into the second?

For example:

First file has these numbers: 10 1 5 -3 -9 -12 34 0 2 -52

Second file: -52 -9 0 2 10

Third file: -12 -3 1 5 34 My code:

#include <fstream>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
void main()
{
    srand(time(NULL));
    int n = 10;
    int *a = new int[n];
    ifstream f("numbers.txt");
    ofstream g("AscNum1.txt");
    ofstream h("AscNum2.txt");
    while (f.is_open()) {
        for (int i = 0; i < n; i++) {
            f >> a[i]; //reading numbers from file
        }
        sort(a, a + n);
        for (int i = 0; i < n; i++) {
            if (i % 2 == 0) {
                g << a[i] << ' ';//writing number if its index is even
            }
            else
                h << a[i] << ' '; //writing number if its index is not even
        }

        h.close();
        g.close();
        f.close();
    }
    
}

In this code, i sort before "transferring" and "transfer" by even index(Second file contains numbers with even indexes).

How can I "transfer" without using sort? How can I make this by different way?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum