TADM2E 4.9

From Algorithm Wiki
Revision as of 18:14, 11 September 2014 by Algowikiadmin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

1) Sort the sets (which takes <math>O(n \log{}n)</math>) and use the algorithm described in 2) which takes <math>O(n)</math> (which is also <math>O(n \log{}n)</math>).

2) A and B are sorted (assume in ascending order). The fact that the sets are sorted implies that there is a comparison defined on the elements of A and B (i.e. we can tell whether an element is greater, equal to or smaller than another element).

  • let U be the set which will contain the union
  • while A and B are not empty:
    • if the first element of A is equal to the last element added to U, remove the first element of A and continue with the next iteration
    • if the first element of B is equal to the last element added to U, remove the first element of B and continue with the next iteration
    • if the first (lowest) element of A is strictly smaller than the first (lowest) element of B, remove the first element of A and add it to U, then continue with the next iteration
    • if the first (lowest) element of A is strictly greater than the first (lowest) element of B, remove the first element of B and add it to U, then continue with the next iteration
    • if the first (lowest) element of A is equal to the first (lowest) element of B, remove the first element from each A and B and add one of them to U, then continue with the next iteration
  • After the while loop, either A or B or both are empty. If one of them is non-empty, add its elements to U.