# Differentiating between BubbleSort and InsertionSort

This is a homework I'm doing, but I couldn't find an answer, hopefully you guys can shine some light on this.

The problem is this:
You have two unknown sorting algorithms, one is Bubble Sort, the other Insertion Sort.
Identify which is which given that you can only differentiate them via differences in asymptotic time for different data sets. (Meaning no run time cross comparison between the two sorts, as our professor may add constant time costs making Insertion Sort slower).

Edit: To clarify, we can specify any array of length n, with a particular sequence if you wish, to see the time it took to sort.
However, you may not compare times between the two sorts. E.g. This sort is faster, therefore this is Insertion Sort.

My findings:

            Insertion Sort | Bubble Sort
Best case:            O(n) | O(n)
Worst case:         O(n^2) | O(n^2)
Average case:       O(n^2) | O(n^2)
Stable:                Yes | Yes
In-place:              Yes | Yes
Nearly Sorted:        O(n) | O(n)
Reversed:           O(n^2) | O(n^2)
Few Unique: closer to O(n) | closer to O(n^2) // not sure


Given how similar the two sorts are, it's really hard to distinct them...

• I find the problem statement unclear. Exactly what is allowed? What does the problem statement mean by "asymptotic time for different data sets"? Does it mean you are allowed to provide an input to the sorting algorithm, and then observe how long the algorithm takes? But asymptotic running time is not about a single data set, but rather an infinite sequence of data sets. So exactly what form of differentiation is allowed? Can you edit to clarify? If you're not sure, then it sounds like your next step is to ask your instructor what he/she intended by the question. – D.W. Jan 30 '16 at 5:30
• @D.W. We are allowed to compare data sets run time for each sort, meaning we can compare run times for n = 100 vs n = 1000000 to see if the sort did it in O(n) or O(n^2) for let's say, a reversed sorted data set. – Kai Jan 30 '16 at 5:34
• I stand by my prior comment. Looking at the running time for finite values of n does not tell you what the asymptotic running time is. You can't tell whether a program is $O(n)$ or $O(n^2)$ by looking at running time for a few finite values of $n$. Asymptotic running time, by definition, refers to the behavior of the running time for a set of infinitely many input sizes. It sounds like the problem is not well-posed. Perhaps we can try to guess what might have been intended, but if we take the wording literally, it does not seem well-defined. – D.W. Jan 30 '16 at 5:50
• Anyway, even with your comment, it's still not exactly clear what we are allowed to do. Are we allowed to specify a specific input and see the running time on that input? To specify a value of $n$ and see the maximum running time over all arrays of length $n$? To specify an infinite sequence of inputs and learn the asymptotic running time for that sequence of inputs? Something else? It's not clear. Please edit the question to clarify exactly what we are allowed to do and exactly what value we learn in response. – D.W. Jan 30 '16 at 5:52
• The problem is ill-posed. You can not hope to get asymptotics from a finite sample of running times. – Raphael Jan 30 '16 at 13:07

Unfortunately, your problem statement is contradictory, so the problem cannot be solved in its current form. Your question states that we can obtain the running time for finite values of $n$. However, it also states that we only learn the asymptotic running time. This is self-contradictory, because it is not possible to infer the asymptotic running time based on any finite number of measurements.
If I had to speculate, I'd guess that maybe what the instructor actually wants you to do is find an infinite sequence of inputs $a_1,a_2,a_3,\dots$ such that: (1) $a_i$ is an array of length $i$; (2) the asymptotic running time of Insertion Sort on these inputs differs by more than a constant factor from the asymptotic running time of Bubble Sort on these inputs. If that's correct, your next step should be to investigate various ways you might construct such a sequence. I emphasize that this is only speculation, and not something that can be definitively derived from the problem statement as provided.