[英]efficient union of n sorted arrays in C++ (set vs vector)?
我需要實現一種有效的算法,以從多個排序的數組中查找一個排序的聯合。 由於我的程序會執行許多此類操作,因此我使用C ++進行了模擬。 我的第一種方法(method1)是簡單地創建一個空向量,並將其他向量中的每個元素附加到該空向量上,然后使用std :: sort和std :: unique獲得所有元素的想要排序的並集。 但是,我認為將所有矢量元素轉儲到集合(方法2)中可能會更有效,因為集合已經使它們變得唯一並可以一次性排序。 令我驚訝的是method1比method2快5倍! 我在這里做錯什么了嗎? method2是否應該更快,因為它執行較少的計算? 提前致謝
//// method1與向量:
std::vector<long> arr1{5,12,32,33,34,50};
std::vector<long> arr2{1,2,3,4,5};
std::vector<long> arr3{1,8,9,11};
std::vector<long> arr;
int main(int argc, const char * argv[]) {
double sec;
clock_t t;
t=clock();
for(long j=0; j<1000000; j++){ // repeating for benchmark
arr.clear();
for(long i=0; i<arr1.size(); i++){
arr.push_back(arr1[i]);
}
for(long i=0; i<arr2.size(); i++){
arr.push_back(arr2[i]);
}
for(long i=0; i<arr3.size(); i++){
arr.push_back(arr3[i]);
}
std::sort(arr.begin(), arr.end());
auto last = std::unique(arr.begin(), arr.end());
arr.erase(last, arr.end());
}
t=clock() - t;
sec = (double)t/CLOCKS_PER_SEC;
std::cout<<"seconds = "<< sec <<" clicks = " << t << std::endl;
return 0;
}
//// method2帶有集合:
std::vector<long> arr1{5,12,32,33,34,50};
std::vector<long> arr2{1,2,3,4,5};
std::vector<long> arr3{1,8,9,11};
std::set<long> arr;
int main(int argc, const char * argv[]) {
double sec;
clock_t t;
t=clock();
for(long j=0; j<1000000; j++){ //repeating for benchmark
arr.clear();
arr.insert(arr1.begin(), arr1.end());
arr.insert(arr2.begin(), arr2.end());
arr.insert(arr3.begin(), arr3.end());
}
t=clock() - t;
sec = (double)t/CLOCKS_PER_SEC;
std::cout<<"seconds = "<< sec <<" clicks = " << t << std::endl;
return 0;
}
這是使用2個向量完成的方法。 您可以輕松地將過程概括為N個向量。
vector<int> v1{ 4, 8, 12, 16 };
vector<int> v2{ 2, 6, 10, 14 };
vector<int> merged;
merged.reserve(v1.size() + v2.size());
// An iterator on each vector
auto it1 = v1.begin();
auto it2 = v2.begin();
while (it1 != v1.end() && it2 != v2.end())
{
// Find the iterator that points to the smallest number.
// Grab the value.
// Advance the iterator, and repeat.
if (*it1 < *it2)
{
if (merged.empty() || merged.back() < *it1)
merged.push_back(*it1);
++it1;
}
else
{
if (merged.empty() || merged.back() < *it2)
merged.push_back(*it2);
++it2;
}
}
while(it1 != v1.end())
{
merged.push_back(*it1);
++it1;
}
while (it2 != v2.end())
{
merged.push_back(*it2);
++it2;
}
// if you print out the values in 'merged', it gives the expected result
[2, 4, 6, 8, 10, 12, 14, 16]
...並且您可以概括以下內容。 請注意,同時包含“當前”迭代器和最終迭代器的幫助程序結構會更清潔,但思路仍然相同。
vector<int> v1{ 4, 8, 12, 16 };
vector<int> v2{ 2, 6, 10, 14 };
vector<int> v3{ 3, 7, 11, 15 };
vector<int> v4{ 0, 21};
vector<int> merged;
// reserve space accordingly...
using vectorIt = vector<int>::const_iterator;
vector<vectorIt> fwdIterators;
fwdIterators.push_back(v1.begin());
fwdIterators.push_back(v2.begin());
fwdIterators.push_back(v3.begin());
fwdIterators.push_back(v4.begin());
vector<vectorIt> endIterators;
endIterators.push_back(v1.end());
endIterators.push_back(v2.end());
endIterators.push_back(v3.end());
endIterators.push_back(v4.end());
while (!fwdIterators.empty())
{
// Find out which iterator carries the smallest value
size_t index = 0;
for (size_t i = 1; i < fwdIterators.size(); ++i)
{
if (*fwdIterators[i] < *fwdIterators[index])
index = i;
}
if (merged.empty() || merged.back() < *fwdIterators[index])
merged.push_back(*fwdIterators[index]);
++fwdIterators[index];
if (fwdIterators[index] == endIterators[index])
{
fwdIterators.erase(fwdIterators.begin() + index);
endIterators.erase(endIterators.begin() + index);
}
}
// again, merged contains the expected result
[0, 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, 16, 21]
...而且正如一些人指出的那樣,使用堆會更快
// Helper struct to make it more convenient
struct Entry
{
vector<int>::const_iterator fwdIt;
vector<int>::const_iterator endIt;
Entry(vector<int> const& v) : fwdIt(v.begin()), endIt(v.end()) {}
bool IsAlive() const { return fwdIt != endIt; }
bool operator< (Entry const& rhs) const { return *fwdIt > *rhs.fwdIt; }
};
int main()
{
vector<int> v1{ 4, 8, 12, 16 };
vector<int> v2{ 2, 6, 10, 14 };
vector<int> v3{ 3, 7, 11, 15 };
vector<int> v4{ 0, 21};
vector<int> merged;
merged.reserve(v1.size() + v2.size() + v3.size() + v4.size());
std::priority_queue<Entry> queue;
queue.push(Entry(v1));
queue.push(Entry(v2));
queue.push(Entry(v3));
queue.push(Entry(v4));
while (!queue.empty())
{
Entry tmp = queue.top();
queue.pop();
if (merged.empty() || merged.back() < *tmp.fwdIt)
merged.push_back(*tmp.fwdIt);
tmp.fwdIt++;
if (tmp.IsAlive())
queue.push(tmp);
}
. 不過,似乎確實復制了許多“ Entry”對象,對於 ,具有適當比較功能的條目指針可能會更好。
合並許多隊列的通常方法是根據隊列中第一個元素的值將其放入最小堆中。 您反復從堆頂部的隊列中拉出一個項目,然后將其向下推以恢復堆屬性。
這將在O(N log K)時間內合並總共N個項目K個隊列。
由於您正在合並vector<int>
,因此您的隊列可以是tuple<int, vector *>
(當前位置和向量)或tuple<vector::const_iterator, vector::const_iterator>
(當前位置和結束)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.