The complexity of the following code segment Show your work

The complexity of the following code segment? (Show your work.) void Unsorted Type::Delete item (int a, float b) a = a/3 b = a; for (int i = 0; i 0) b = 100

Solution

1.   void UnsortedType::DeleteItem(int a, float b)
2.   {
3.           a = a / 3;
4.           b = a;
5.           for(int i=0;i<n;i++)
6.           {
7.                   info[i] = info[length - 1];
8.                   length--;
9.           }
10.           if(b>0)
11.               b = 100;
12.   }

Complexity Analysis -

   complexity of line 2, 3 : O(1) as this lines are execute atmost 1 time
  
   complexity of for loop in line 5 to line 9 : O(n) as this for loop executes atmost n times
  
   complexity of if block in line 10 to line 11 : O(1) as it executes only 1 time

   Thus, complexity of the full function is :
      
       2 * O(1) + O(n) + O(1) = O(n)
      
   Therefore, complexity of this function is : O(n)

 The complexity of the following code segment? (Show your work.) void Unsorted Type::Delete item (int a, float b) a = a/3 b = a; for (int i = 0; i 0) b = 100Sol

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site