Question

I just wrote the quick and merge sort algorithms and I want to make a log-log plot of their run time vs size of array to sort.

As I have never done this my question is does it matter if I choose arbitrary numbers for the array length (size of input) or should I follow a pattern (something like 10^3, 10^4, 10^5, etc)?

Was it helpful?

Solution

In general, you need to choose array lengths, for each method, that are large enough to display the expected o(n log n) or O(n^2) type behavior.

If your n is too small the run time may be dominated by other growth rates, for example an algorithm with run time = 1000000*n + n^2 will look to be ~O(n) for n < 1000. For most algorithms the small n behavior means that your log-log plot will initially be curved.

On the other hand, if your n is too large your algorithm may take too long to complete.

The best compromise may be to start with small n, and time for n, 2n, 4n,..., or n, 3n, 9n,... and keep increasing until you can clearly see the log log plots asymptoting to a straight lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top