- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面使用partition的quickSort方法有什么问题?第 N 个元素似乎工作正常,但我认为分区也可以。我看到一个有 2 个分区调用的示例,但我不应该只需要一个吗?
#include <iostream>
#include <algorithm>
#include <iterator>
template <typename It>
void quickSortWorks (const It& lowerIt, const It& upperIt)
{
auto d = upperIt - lowerIt ;
if ( d < 2 )
return;
auto midIt = lowerIt + d / 2;
std::nth_element ( lowerIt, midIt, upperIt);
quickSortWorks( lowerIt, midIt );
quickSortWorks( midIt+1, upperIt );
}
template <typename It>
void quickSort (const It& lowerIt, const It& upperIt)
{
auto d = upperIt - lowerIt ;
if ( d < 2 )
return;
auto midIt = lowerIt + d / 2;
auto pIt = std::partition ( lowerIt, upperIt, [midIt](int i) { return i < *midIt; } );
quickSort( lowerIt, pIt );
quickSort( pIt + 1, upperIt );
}
int main ( )
{
unsigned int N = 10;
std::vector<int> v(N);
// srand (time(nullptr));
for_each(v.begin(), v.end(), [](int& cur){ cur = rand()%100; });
std::vector<int> vorig(v);
auto print_vec = [](std::ostream& out, const std::vector<int>& v) {
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(out, ", "));
out << std::endl;
};
std::cout << " Using Partition: " << std::endl;
std::cout << " Before: " << std::endl;
print_vec(std::cout,v);
quickSort(v.begin(), v.end());
std::cout << " After: " << std::endl;
print_vec(std::cout,v);
v = vorig;
std::cout << " Using Nth Element: " << std::endl;
std::cout << " Before: " << std::endl;
print_vec(std::cout,v);
quickSortWorks(v.begin(), v.end());
std::cout << " After: " << std::endl;
print_vec(std::cout,v);
}
输出:
Using Partition:
Before:
83, 86, 77, 15, 93, 35, 86, 92, 49, 21,
After:
21, 15, 77, 35, 49, 83, 86, 92, 86, 93,
Using Nth Element:
Before:
83, 86, 77, 15, 93, 35, 86, 92, 49, 21,
After:
15, 21, 35, 49, 77, 83, 86, 86, 92, 93,
最佳答案
所编写的代码只是意外地起作用,原因如下,
std::partition
通过谓词完成它的工作,前向序列包含计算结果为 true 的元素,其余元素计算结果为 false。这意味着 std::partition
将大于基准的元素和等于基准的元素视为等效。
无法保证序列 [middle,last)
的顺序!
显然这不是您想要的。您希望将等于 pivot 的元素压缩到序列 [middle,last)
的前面。这就是为什么您查看的示例代码使用了第二个分区,以将此排序强加于尾随序列(至少您需要将主元元素置于正确的位置)。
为了清楚起见,
template<typename Ran>
void quicksort(Ran first, Ran last)
{
typedef typename std::iterator_traits<Ran>::value_type value_type;
if (last - first < 2)
return;
Ran middle = first + (last - first)/2;
// save pivot.
std::iter_swap(middle, last-1);
middle = std::partition(first, last-1,
[last] (const value_type& x) { return x < *(last-1); });
// restore pivot.
std::iter_swap(middle, last-1);
quicksort(first, middle);
quicksort(middle+1, last);
}
关于c++ - 将 std::partition 与快速排序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072808/
我有什么: 简单的服务器,配备一个具有 8 个逻辑内核的至强处理器、16 GB 内存、2 个 7200rpm 驱动器的 mdadm raid1。 PostgreSQL 需要处理大量数据。每天导入多达
当消息排入分区队列时,服务总线会检查分区键是否存在。如果找到,它将根据分区键选择片段。 但是当该片段已满时会发生什么,该片段中没有剩余空间。服务总线是否给出错误/消息被丢弃或任何其他片段将用于存储该消
我想知道将集合拆分为子集的有效方法是什么? Iterable> partitions = Iterables.partition(numbers, 10); 或 List> partitions =
有人可以告诉我 DATETIME 列上 HASH PARITION 与 RANGE PARTITION 的优缺点吗?假设我们有一个包含 2000 万条记录的 POS 表,并且想要根据交易日期的年份创建
我们有一个 cosmos-db 容器,其中包含大约 1M 条记录,其中包含有关客户的信息。 documentDb 的分区键是 customerId,它保存客户的唯一 GUID 引用。我已阅读parti
因此,我在写入数据库的步骤中有 2 个分区。我想记录每个分区写入的行数,得到总和,打印到日志中; 我正在考虑在编写器中使用static变量,并使用Step Context/Job Context在St
Bigquery 文档说可以更新分区表的分区时间到期。而我只能为摄取时间分区表执行此操作。我尝试了以下方法: bq query --use_legacy_sql=false ' CREATE
这是一个两部分的问题: 1) 是否可以根据数据的 ROWID 或其他标识符使用 select 语句检索数据所在分区的名称? 例如。 SELECT DATA_ID, CATEGORY, VALUE, *
注意:这几乎是 this question 的副本区别在于,在这种情况下,源表是日期分区的,而目标表尚不存在。此外,该问题的公认解决方案在这种情况下不起作用。 我试图将一天的数据从一个日期分区表复制到
我已经搜索了很多,但找不到有关以下场景的任何信息。 考虑一个包含超过 500,000 行、约 20 列和约 5 列上的 INDEX 的 InnoDB 表。 当该表处于以下情况时,执行“ALTER TA
如何将分区表(在 Oracle 10g 数据库中)更改为不仅用于分区而且还用于表本身的新表空间?我的意思是,我可以毫无问题地进行以下操作, --sql 改变表 abc 移动分区 abc01 表空间 n
我们正在尝试基于 BigQuery 在云中构建(或者更好地说重建)我们的 DWH。我们决定对原始数据使用“按日期字段分区”表(如“created_date”字段),而不是摄取时间分区,因为通过此功能,
给定一个使用分区的 Spring Batch 作业:
“每个分区中可以有许多键(及其相关值),但任何给定键的记录都在一个分区中。”这是一本著名的hadoop教科书的一行。我没有理解它的第二部分的全部含义,即“但是任何给定键的记录都在一个分区中。”这是否意
Let's say I have an Athena table mytable partitioned by columns A, B, and C.假设我有一个由列A、B和C分区的Athen
我正在寻找一些文档来了解 hive.exec.max.dynamic.partitions 和 hive.exec.max.dynamic.partitions.pernode 之间的区别。 我们什么
我看过一些关于创建分区的表的很好的解释,这些分区是 CLUSTERED BY 和 SORTED BY。这与创建带分区的表,然后使用 CLUSTER BY 填充表(例如使用 INSERT OVERWRI
使用摄取时间分区表,可以免费查询每个分区的行数。字节计费为 0。 SELECT DATE(_PARTITIONTME) AS dd, COUNT(*) FROM ds.ingestion_time_p
此处提供示例项目: https://github.com/codependent/micronaut-aws-lambda-proxy-graal 我在 Amazon AWS 上部署了一个 Micro
是否可以在不指定分区键的情况下通过其 ID 检索文档? 我的理解来自阅读 documentation是当未指定分区键时查询将在所有分区中扇出: The following query does not
我是一名优秀的程序员,十分优秀!