Two loop algorithm complexity

Asked

Viewed 62 times

-1

What would be the complexity function and the Big O of the code below?

int[] alg4(int[] arr1, int[] arr2) {
  int i = 0;
  int j = 0;
  int[] arr3 = new int[arr1.Length+arr2.Length];

  for (i = 0; i < arr1.Length; i++)
  arr3[j++] = arr1[i];

  for (i = 0; i < arr2.Length; i++)
  arr3[j++] = arr2[i];

  return arr3;
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

The complexity is linear, which is usually represented by O(n). And can be calculated with expression that is already in code, the n complexity is precisely the sum of the sizes of the two arrays. Every time there is such a simple loop, complexity is always linear, and in the case how it has two loops is the sum of them. It would be different if they were nesting.

  • It might even represent how o(|arr1| + |arr2|)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.