Time Complexity of the "union" function

I am using the "union" function to combine two arrays "A" and "B", could you provide me with some information on the time complexity of this function?
Additionally, what type of sorting algorithm does the function use, and is there a difference in the time complexity if the input arrays are already pre-sorted?

 Accepted Answer

The "union" function uses a Quicksort sorting algorithm, which has the following time complexity:
  • best case O(n),
  • average case O(n log(n))
  • worst case O(n^2)
Additionally, "union" concatenates the input arrays, which means that pre-sorting the inputs will not affect the time complexity of the function.
The only exception is the case where the maximum element of one array is less than or equal to the minimum element of the other array.

More Answers (0)

Categories

Products

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!