What Big-O notation would this fall under? I know setSearch() and removeAt() are of order O(n) (assume they are either way). I know without the for loop it’d be O(n), for certain, but I get confused how to calculate what it becomes with a for loop thrown into it. I’m not all that great at math… so. Would it be O(n^2)?
public void removeAll(DataElement clearElement)
{
if(length == 0)
System.err.println("Cannot delete from an empty list.");
else
{
for(int i = 0; i < list.length; i++)
{
loc = seqSearch(clearElement);
if(loc != -1)
{
removeAt(loc);
--i;
}
}
}
}
If removeAt() and seqSearch() are O(n) with respect to the length of the list then yes, this algorithm would be of order O(n^2). This is because within the for loop you call seqSearch every time, with a possibility of calling removeAt(loc). That means for each iteration you are doing either n or 2n operations. Taking the worst case, you have 2n^2 operations which is O(n^2).