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?

 채택된 답변

MathWorks Support Team
MathWorks Support Team 2022년 4월 7일

0 개 추천

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by