Please help for Big O analysis

Hi I have written a piece of code for this problem:

5. Given an input set S containing n real numbers and a real number x. Design an algorithm that determines whether there are two numbers in S whose sum is exactly x. Submit your algorithm and its time complexity analysis in terms of the big O.

Here is my code
for (i = 0 ; i < length; i ++)
for (j = i +1; j < length; j++)
if (array.i + array.j = x)
cout << i << j ;

My question is, I don't know if my code is O(n^2) or O(n log n)
Can someone please tell me which one it is....
Thank you very much
For an iteration of the outer loop, what is the average number of comparisons that you are doing?
for (i = 0 ; i < length; i ++)
{
for (j = i +1; j < length; j++)
if (array.i + array.j = x)
{cout << i << j ; }
}

the whole thing should be like this.
So for outer loop, say i have an array of 5, array [1,2,3,4,5]
outer loop to put i point to array[0], and examine second loop
which it add array[0] and [1], [0] and [2], [0] and [3], and [0] and [4]
after that, i is incremented, and examine again
[1] and [2] , [1] and [3], [1] and [4], and so on

but I am confused about whether this algorithm is in O(n^2) or in O(n log n)
I guess the average number of comparison I am doing is depended on the amount of input n the algorithm is dealing with
> I am confused about whether this algorithm is in O(n^2) or in O(n log n)
don't guess, explain the rationale behind each of your answers.


Count the operations, if it's too hard to do, simulate.
Topic archived. No new replies allowed.