Given several sets of elements, e.g.:
int set1[5] {5601, 935, 4153, 2195, 422};
int set2[5] {5601, 935, 23, 44, 422};
int set3[5] {4205, 935, 4153, 2195, 15};
int set4[5] {4205, 589, 4015, 44, 422};
Where order matters (i.e. 1, 2, 3 is different to 2, 1, 3), what is an efficient algorithm to locate a specific set? E.g., you want to locate:
int value[5] {5601, 935, 23, 44, 422};
Considerations:
-
Insertion cost for new sets is not an issue, so they can be stored in any data structure, in order to optimize search time.
-
The sets will contain anywhere between 1 and 1,000,000 elements each (approximately, and there will be anywhere between 1 and 1000 sets (again, approximately). However the number of elements will always be the same for any given set of sets (e.g. if one set has 10 elements, then all sets will have 10 elements).
Follow-up question, I’ll be implementing this in C++ so I’d be interested to find out for any recommended algorithms, whether they exist in an open source C++ library (preferably STL, Boost, or QT, but I’ll consider others too).
Use a
std::vector<set_type>to store the sets. Insert all of the sets into the container. Sort the container usingstd::sort. Find elements usingstd::binary_search(orstd::lower_boundif you need an iterator to the element).The type you use for
set_typedepends on the number of elements in each set. If the number of elements is known to be small, thenstd::array<T, N>would be sufficient; otherwise, considerstd::vector<T>.