1

Не могу понять где теряет элементы файла после сортировки.

void MainWindow::Simple(const QString &filePath)
{
    on_action_2_triggered();// записать с формы

    int k, i, j, size = 0 ;
    QString str1,str2;
    QFile f(filePath),f1(".//smsort_1.txt"),f2(".//smsort_2.txt");

    QTextStream stream(&f), stream1(&f1), stream2(&f2);

    f.open(QIODevice::ReadOnly | QIODevice::Text);

    while (!stream.atEnd())
    {
        str1 = stream.readLine();
        size++;
    }
    f.flush();
    f.close();

    k=1;

    unsigned int start_time =  clock();

    while(k<size)
    {
        f.open(QIODevice::ReadOnly | QIODevice::Text);
        f1.open(QIODevice::WriteOnly | QIODevice::Text);
        f2.open(QIODevice::WriteOnly | QIODevice::Text);

        if(!stream.atEnd()) str1 = stream.readLine();

        while(!stream.atEnd())
        {
            for( i = 0; i < k && !stream.atEnd(); i++)
            {
                stream1 << str1 + "\n";
                str1 = stream.readLine();
            }

            for( j = 0; j < k && !stream.atEnd(); j++)
            {
                stream2 << str1 + "\n";
                str1 = stream.readLine();
            }
        }

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

        f.open(QIODevice::WriteOnly | QIODevice::Text);
        f1.open(QIODevice::ReadOnly | QIODevice::Text);
        f2.open(QIODevice::ReadOnly | QIODevice::Text);

        if(!stream1.atEnd()) str1 = stream1.readLine();
        if(!stream2.atEnd()) str2 = stream2.readLine();

        while(!stream1.atEnd() && !stream2.atEnd())
        {
            i=0;
            j=0;

            while (i < k && j < k && !stream1.atEnd() && !stream2.atEnd())
            {
                if (V_on(str1,str2))
                {
                    stream << str1 + "\n";
                    str1 = stream1.readLine();
                    i++;
                }
                else
                {
                    stream << str2 + "\n";
                    str2 = stream2.readLine();
                    j++;
                }
            }

            while ( i < k && !stream1.atEnd())
            {
                stream << str1 + "\n";
                str1 = stream1.readLine();
                i++;
            }

            while (j < k && !stream2.atEnd())
            {
                stream << str2 + "\n";
                str2 = stream2.readLine();
                j++;
            }
        }

           while (!stream1.atEnd())
            {
                stream << str1 + "\n";
                str1 = stream1.readLine();
            }

            while (!stream2.atEnd())
            {
                stream << str2 +"\n";
                str2 = stream2.readLine();
            }

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

        k*=2;
    }

    unsigned int end_time =  clock();
    unsigned int search_time = end_time - start_time;
    ui->statusBar->showMessage("Выполнено за " + QString::number(search_time)+" ms");

    Out_file(filePath); // вывод 
}
0

0

Посмотрите другие вопросы с метками или задайте свой вопрос.