Hi,

Quick question, trying to get the Big O notation of a simple function. Would this be O(n)? Or would the notation be something like O(number of lines in somefile) ?

Thanks for the help.

void createAffiliateNodes()
	{
		ifstream file;
                file.open("somefile");

		while ( getline(file, line) )
		{
			
		}

		file.close();
	}

That is a O(n) - it runs in linear time.

We don't consider how many lines in the file - the idea of O() notation is to generalize how the work grows in relation to the problem size. Whether on particular algorithm does something in three operations per step and another in four operations only means that the constant varies, but proportionately they are the same.

Now, if you are doing something in the loop where the work compounds in some fashion, the evaluation of the algorithm might change.

Val

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.