Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

I have 2 large arrays of objects, what is more efficient algorithm to know they contain the exact same members other than the nested loop comparison?

0
Posted

I have 2 large arrays of objects, what is more efficient algorithm to know they contain the exact same members other than the nested loop comparison?

0

1) first thing, compare length, if not equal, gone! 2) Make copy of each, O(n) 3) Sort both copy, O(n * lg n) 4) Compare each pair O(n) Over all O(n * lg n)It is much better than nested loop, which is O(n*n). If the n = 13,000,000,000, use your calculator, you will see huge speed difference. Of course, if n = 130, the nested loop algorithm is simple and better. Since the time saving on computing does not worth the programming effort and the overhead of the complicated algorithms.How to compare two Objects? Define a java.util.Comparator for the object you want to compare, and then the algorithm would be the same.Since speed is the major concern, do not use Vector, use array instead, according to some research; Vector is at least 3 times slower than straight array.Here is an example of how to use Comparator: http://www.geocities.com/developergrp/Code/SortBy.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123