tat2dlady 0 Light Poster

I am doing a 5-way balanced sort merge. I have 1, 000, 000 records and a toal of 10 temp files to work with (5 for input and 5 for output). After phase 1, the temp files F0 thru F4 are populated with clumps of sorted records. The clump size is 1000. I am using an array of files to work with, instead of hard coding 10 temp files. My phase 2 is not working correctly. What I am trying to do is read 1 record from each of the 5 temp files and write the smallest one to the corresponding output file. I am having trouble with what controls the loop to do this phase as well. The total number of runs 5.

Here is the main part of my code:

int main()
{
   cout << '\n';
  cout << "**********Phase 2 Started**********" << '\n';
  cout << '\n';

  Merges = ceil ((NumOfColumns / NumOfFiles));
  ClumpSize = ClumpSize * NumOfFiles;
  F[Counter] >> Number1;
     
  F[Counter+1] >> Number2;
     
  F[Counter+2] >> Number3;
     
  F[Counter+3] >> Number4; 
    
  F[Counter+4] >> Number5;
      
  if (Counter == 5)
  {
     Counter = 0;
  }
    
while (PassCount < NomOfPasses)
{
   for (i = 0; i < Merges; i++)
   {
      for (j = 0; j < NumOfFiles; j++)
      {
        for (k = 0; k < ClumpSize; k++)
        {
        if ((Number1 < Number2) && (Number1 < Number3) && (Number1 < Number4) && (Number1 < Number5))
        {
           X.WhichFile = 1;
           X.Priority = -1;
           X.Number = Number1;
           P.Insert(X);
           F[Counter] >> Number1;
           if (F[Counter].eof())
           {
             break;
           }
        }
        else if ((Number2 < Number1) && (Number2 < Number3) && (Number2 < Number4) && (Number2 < Number5))
        {
           X.WhichFile = 2;
           X.Priority = -2;
           X.Number = Number2;
           P.Insert(X);
           F[Counter+1] >> Number2;
           if (F[Counter+1].eof())
           {
             break;
           }
         }
         else if ((Number3 < Number1) && (Number3 < Number2) && (Number3 < Number4) && (Number3 < Number5))
         {
           X.WhichFile = 3;
           X.Priority = -3;
           X.Number = Number3;
           P.Insert(X);
           F[Counter+2] >> Number3;
           if (F[Counter+2].eof())
           {
             break;
           }
         }
         else if ((Number4 < Number1) && (Number4 < Number2) && (Number4 < Number3) && (Number4 < Number5))
         {
           X.WhichFile = 4;
           X.Priority = -4;
           X.Number = Number4;
           P.Insert(X);
           F[Counter+3] >> Number4;
           if (F[Counter+3].eof())
           {
             break;
           }
         }
         else if ((Number5 < Number1) && (Number5 < Number2) && (Number5 < Number3) && (Number5 < Number4))
         {
           X.WhichFile = 5;
           X.Priority = -5;
           X.Number = Number5;
           P.Insert(X);
           F[Counter+4] >> Number5;
           if (F[Counter+4].eof())
           {
             break;
           }
         }
         P.Remove(X);
         F[OCounter] << setprecision(15) << X.Number << '\n'; 
      }
      // end for
    
      OCounter ++;
    
      if (OCounter == 10)
      {
        OCounter = 5;
      }
      // end if
  }
  // end for
  NewNumOfFiles = OCounter - NumOfFiles;
  
  ClumpSize = ClumpSize * NewNumOfFiles;

  CloseTheFiles (F, FileName, InputFileName, InputFile);
  if (OCounter == 5)
  {
    OpenTempFilesAsOutput(F, FileName);
    OCounter = 0;
    Counter = 5;
  }
  else if (OCounter == 0)
  {
    OpenTempFilesAsInput(F, FileName);
    OCounter = 5;
    Counter = 0;
  }
    
  PassCount++;
  cout << "Pass " << PassCount << " completed." << '\n';
  }
  // end while

  return 0;
} // end function main

When I run it, I get all passes complete at the same time. It should tell me when one pass is complete. It should take 4 passes. Like it is, it leaves out some numbers. I get like 95,000 in 3 files but 90,000 in the other 2. This problem has just kicked my butt! Thanks for any input.

<< moderator edit: added [code][/code] tags >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.