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?

 Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 7 Avr 2022

0 votes

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.

Plus de réponses (0)

Catégories

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by