Code snippet
Following is the delete
function definition to delete all the occurrences of an element x in an int
type array named a
in C language!
void delete(int x)
{
for(int i=0 ; i<size ; i++)
{
if (a[i] == x)
{
for(int j=i+1;j<size;j++)
{
a[j-1]=a[j];
}
size--;
} /* end of if */
} /*end of outer for*/
}
The time complexity of above code is coming out to be O(n2) , quadratic complexity .
My question : The outer for loop will run n times and the number of times the inner for loop runs would depend on the number of occurrences of x then how is the complexity coming out to be O(n2) ?