简体   繁体   中英

Big-O of finding common elements in sorted and unsorted arrays

Having a difficult time grasping big O when it comes to printing out the common elements in two arrays. On of size a, the other of size b.

I know two unsorted arrays is O(ab)

But what about when a is unsorted and b is sorted?

When both are sorted?

Any explanation would be great.

An efficient way to do this is to use a hash table ( HashMap in Java). An algorithm is:

foreach element of the smallest array  O(N)
  add element to the hash table        O(1)

foreach element of the biggest array   O(M)
  if element is in the hash table      O(1)
    print element

The time complexity of every step is annotated in the pseudo code above.

  • Iterating over the elements of an array of size X has a time complexity of O(X) and a constant memory complexity (the size of an element)
  • Inserting an element in a hash table has a constant time and space complexity
  • Checking if an element is in a hash table has constant time and space complexity

So the overall time complexity is O(N + M) , and the space complexity is O(N)

Note that

  • this complexity is not influenced by whether the arrays are sorted or not
  • this algorithm works only if your smallest array can fit in memory
  • it is better to build the hash table using the smallest array, because it will reduce in a smaller hash table in memory

Hope that helps!

I'm assuming that you want to use $O(1)$ additional memory, because otherwise, you could solve either problem (sorted or unsorted) in $O(a + b)$ by using a hash table. Also, I'm going to assume the elements in each array are unique, but the algorithms can easily be mended if not.

If both are unsorted, you can in fact improve on your approach by simply sorting one of the arrays, then using the approach below. This gives $O((a+b)\\min(\\lg a, \\lg b)$.

If a is unsorted and b is sorted, you can iterate for all elements in a, and check if they're in b by using binary search . This gives an $O(b + a\\lg b)$ approach.

If both are sorted, you can easily get $O(a + b)$ by using a simple two-pointer algorithm (successively increment indexes on both arrays).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM