I have a quick sort algorithm and a counter that I increment every time a compare or swap is performed. Here are my results for random integer arrays of different sizes –
Array size --- number of operations
10000 --- 238393
20000 --- 511260
40000 --- 1120512
80000 --- 2370145
Edit:
I have removed the incorrect question I was asking in this post. What I am actually asking is –
What Im trying to find out is ‘do these results stack up with the theoretical complexity of quicksort (O(N*log(N)))?
Though you cannot get the asymptotic bound of your method by only experimenting, sometimes you can evaluate its behavior by drawing a graph of the complexities similar to your function, and looking at the behavior.
You can do it with drawing a graph of some functions
y = f(n)such thatf(10000) ~= g(10000)[wheregis your function], and check the behavior difference.In your example, we get the following graphs:
We can clearly see that:
From this, we can deduce that your algorithms is probably
O(n^2)[not strict! remember, big O is not a strict bound], and also could beO(nlogn), if we deduce the difference from theO(nlogn)function is a noise.Notes:
doesn’t give you any worst case [or even average case] bound.
EDIT:
I drew all the graphs as
y1(x) = f(x),y2(x) = g(x), … because I found it easier to explain this way, but usually when you compare two algorithms [as you often actually use this method], the function isy(x) = f(x) / g(x), and you check ify(x)is staying close to 1, growing, shrinking?