这种排序算法已经存在吗?
Does this sort algorithm already exists?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr;
for (int i = 1; i <= 1000; i++)
arr.push_back(rand() % 100000);
//algorithm ----
vector<int> sorted;
sorted.push_back(arr[0]);
for (int i = 1; i < arr.size(); ++i) {
//case: element is smaller than the smallest sorted element
if (arr[i] < sorted[0]) {
sorted.insert(sorted.begin(), arr[i]);
continue;
}
//case: element is inside the sorted elements, search where
if (arr[i] < sorted[sorted.size() - 1]) {
//linear search, really slow
// for (int j = 0; j < sorted.size() - 1; ++j) {
// if (sorted[j] < arr[i] && arr[i] < sorted[j + 1]) {
// sorted.insert(sorted.begin() + j, arr[i]);
// }
// }
//binary search
int low, mid, high;
low = 0;
high = sorted.size() - 2;
while (low <= high) {
mid = (low + high) / 2;
if ( (sorted[mid] < arr[i] && arr[i] < sorted[mid + 1])
|| sorted[mid] == arr[i]
|| sorted[mid + 1] == arr[i]) {
sorted.insert(sorted.begin() + mid, arr[i]);
break;
}
if (sorted[mid] < arr[i])
low = mid + 1;
else
high = mid - 1;
}
if (low > high) {
for (auto x : sorted)
cout << x << " ";
cout << "nsomething broke!n";
}
}
//case: the element is larger than the largest sorted element
//goes to the end
else {
sorted.push_back(arr[i]);
}
}
//----
for(auto x : sorted)
cout << x << " ";
cout << "n";
return 0;
}
前几天,我得到了一个关于实现二叉搜索的排序算法的想法,这是 c++11 中的代码。
我的问题是,是否有人已经制作了这样的算法,或者这甚至是一个可用的算法?
我认为它在 O(n Log n n 附近(,但我不知道插入和push_back函数的复杂性是什么。
任何帮助,不胜感激。 ;)
我还包含了线性搜索的版本,它可能更容易理解,但真的很慢。
我会说这是一个(也许是重新发明的(插入排序版本。 看到这个:
维基百科上的插入排序
你的代码在很长的时候就是这样
if (arr.size() < 2)
return arr;
for (auto i = std::next(arr.begin()); i != arr.end(); ++i) {
std::rotate(std::upper_bound(arr.begin(), i, *i), i, i+1);
}
执行就地插入排序,即 O(N²(。
现在让我们看看你的贡献
if (arr.size() < 2)
return arr;
for (auto i = std::next(arr.begin()); i != arr.end(); ++i) {
if (*i < *arr.begin())
std::rotate(arr.begin(), i, i+1);
else
if (*i > *std::prev(i)) // already in the right spot
continue;
std::rotate(std::upper_bound(arr.begin(), i, *i), i, i+1);
}
所以值得吗,我们获得std::upper_bound
的成本是日志 N,每次 if 是正确的,所以 2if
的成本,情况并非如此。因此,在随机排序的列表中,if
帮助我们的情况将迅速减少,因为每个列表都是 1/n,在接近排序的列表或几乎反向排序的列表的情况下,您会赢得一些,但在所有其他情况下,这只是浪费时间。
相关文章:
- C++模板来检查友元函数的存在
- 二叉排序树无法编译
- 既然存在危险,为什么项目要使用-I include开关
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 我们可以访问一个不存在的联盟的成员吗
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- C++为什么我的指针选择排序中存在分段错误?
- 这种排序算法已经存在吗?
- 是否有用于排序和查找 c++ 中是否已存在的值的函数?
- 我的选择排序代码是否存在导致它跳过数组中的元素的问题?
- 是否存在用于按以下方式对两个范围进行排序和分区的标准算法?
- Bool排序的插入函数,检查列表中是否已经存在int
- 为什么这两个版本的快速排序在进行比较的次数上存在巨大差异
- 插入时对对象进行排序(若存在,则添加数据)
- "std::"具有排序/排序、存在测试和头/尾访问的数据结构?
- 是否存在按主键和从键排序的容器
- 对于未排序的范围,是否存在std::includes等价项
- 使用Vector对子字符串进行降序排序,但存在分割错误
- 是否存在任何键值容器来存储它们的元素而不进行任何重新排序?