hello everyone
i am a fresh man in this site ... and i'm really pleased joining this lovely 'n useful one
well .. i made a program in c++ which is all about natural merge sort on files , since i want to sort my file which contains a randomly generated words (100 , 1000 .... words , according to Nmax value)
so .. i store my data in the file then call the merge sort function
but the problem is that when my data get bigger(i.e. more than 100 words) my program does a partial sorting on'em and it does not print the file in the end at all.. and i think it gets into an infinite loop cuz it does not end successfully ....... so i'm really astonished why!! cuz i'm almost sure that my program is bug-free
so please guyz help me ... and thank you in advance

here is my program bros:

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

int Nmax = 1000;

fstream myfile;


bool compare(char w1[6], char w2[6])
{
	string s1 = w1, s2 = w2;
	if (s1 > s2)
		return true;
	else 
		return false;
}


void distribute(fstream& f1, fstream& f2)
{
	string wrd;
	char w[6];
	myfile.read(w,6);
	do
	{
		f1.write(w,6);
		wrd = w;
		myfile.read(w,6);
		while ((!myfile.eof())&&(w > wrd))
		{
			f1.write(w,6);
			wrd = w;
			myfile.read(w,6);
		}
		if (!myfile.eof())
		{
			f2.write(w,6);
			wrd = w;
			myfile.read(w,6);
			while ((!myfile.eof())&&(w > wrd))
		    {
			    f2.write(w,6);
			    wrd = w;
				myfile.read(w,6);
		    }
		}
	} while(!myfile.eof());
}


int merge(fstream& f1, fstream& f2)
{
	char w1[6], w2[6];
	string wrd;
	bool end_sequence ;
	int nb_sequence = 0;
	f1.read(w1,6);
	f2.read(w2,6);
	while ((!f1.eof())&&(!f2.eof()))
	{		
		end_sequence = false;
		do
		{

		if (compare(w2,w1))
		{			   
			   
				myfile.write(w1,6);
				wrd = w1;
				f1.read(w1,6);
				if (( wrd > w1)||(f1.eof()))
				{
		       	    wrd = w2;
		       	    myfile.write(w2,6);
					f2.read(w2,6);
		      	while ((!f2.eof())&&(w2 > wrd))
		        {
			        myfile.write(w2,6);
			        wrd = w2;
					f2.read(w2,6);
			        
		        }
					end_sequence = true;
				} 
					

		}
		else
		{
			 	   myfile.write(w2,6);
				wrd = w2;
				f2.read(w2,6);
				if ((wrd > w2)||(f2.eof()))
				{
		       	    wrd = w1;
					
					myfile.write(w1,6);
					f1.read(w1,6);
					while ((!f1.eof())&&(w1 > wrd))
		        {
			        myfile.write(w1,6);
			        wrd = w1;
					f1.read(w1,6);
			        
		        }
					end_sequence = true;
				}
				
		}
		}while (!end_sequence);
		nb_sequence++;
	}

	while (!f1.eof())
	{
		wrd = w1;
		myfile.write(w1,6);
		f1.read(w1,6);
		while ((!f1.eof())&&(w1 > wrd))
		{
			myfile.write(w1,6);
			wrd = w1;
			f1.read(w1,6);
		}
		nb_sequence++;
	}
	while (!f2.eof())
	{
		wrd = w2;
		myfile.write(w2,6);
		f2.read(w2,6);
		while ((!f2.eof())&&(w2 > wrd))
		{
			myfile.write(w2,6);
			wrd = w2;
			f2.read(w2,6);
		}
		nb_sequence++;
	}

   return nb_sequence;
}


void Natural_mergeSort()
{
	int nb_sequence;
	fstream t1, t2;
	do
	{
		myfile.open("file1.bin", ios::binary|ios::in);
		t1.open("file2.bin", ios::binary|ios::out);
		t2.open("file3.bin", ios::binary|ios::out);
		t1.clear();
		t2.clear();
		if (!myfile)
		{
			cerr<<"file1 can not be opened"<<endl;
			exit(1);
		}		
		distribute(t1, t2);
		myfile.close();
		t1.close();
		t2.close();
		myfile.open("file1.bin", ios::binary|ios::out);
			myfile.clear();		
			
		t1.open("file2.bin", ios::binary|ios::in);
		t2.open("file3.bin", ios::binary|ios::in);
		if (!t1)
		{
			cerr<<"file2 can not be opened"<<endl;
			exit(1);
		}
		if (!t2)
		{
			cerr<<"file3 can not be opened"<<endl;
			exit(1);
		}
		nb_sequence = merge(t1, t2);
        myfile.close();
		t1.close();
		t2.close();
	} while (nb_sequence!=1);
}

int main()
{
	myfile.open("file1.bin", ios::binary|ios::out);
	myfile.clear();
    if (!myfile)
		{
		cerr<<"file could not be opened"<<endl;
		exit(1);
		}
	char word[6];
	srand(time(0));
	int j, length, i, Cascii;
	j = 1;
	while (j<= Nmax)
	{
			length = 1+rand()%5;
			for(i=0; i<length; i++)
			{
				Cascii= 97+rand()%25;
				word[i] = Cascii;
			}
			word[i] = '\0';
			cout<<word<<endl;
			myfile.write(word,6);
		j++;
	}
	myfile.close();
	cout<<"*************"<<endl;
	Natural_mergeSort();
	myfile.open("file1.bin", ios::binary|ios::in);
	while (!myfile.eof())
	{
		myfile.read(word,6);
		cout<<word<<endl;
	}
    myfile.close();
	return 0;
}

hey Salem i didn't ask my question to get links or somethin
but i just wanna fix my problem plzz
so please if any one know about natural merge sort to help me out

>hey Salem i didn't ask my question to get links or somethin
Of course not. You probably asked with the intention of having someone else find and point out the problems for you. That way you can finish the program and not have to think.

>cuz i'm almost sure that my program is bug-free
This is your biggest problem. Instead of running to us with your astonishment that you can't write bug-free code, you should be digging into a debugger and seeing exactly why the sort gets into an infinite loop. I hate to break it to you, but hunting bugs occupies the majority of a programmer's time. And there won't always be people around to tell you what to do.

... but the problem is that when my data get bigger(i.e. more than 100 words) my program does a partial sorting on'em and it does not print the file in the end at all.. and i think it gets into an infinite loop cuz it does not end successfully

...cuz i'm almost sure that my program is bug-free

Two statements that can't possibly be true. And both in the same paragraph!

Definitions:
bug-free - program runs with no problem and outputs the correct information each and every time.

buggy - the program doesn't work for at least a certain set of data.

....... so i'm really astonished why!!

Because you need a reality check :icon_wink:

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.