确定性选择算法的时间复杂度
Time complexity of a Deterministic Selection algorithm
我最近观看了一段视频,内容是关于如何在 O(n) 时间内制作选择算法 运行,我对制作算法过程中的一个步骤感到困惑。
视频说我们应该将数字集或数组分成 n/5 组,每组 5 个元素,其余元素在另一组中。然后我们找到每组的中位数。然后我们找到中位数的中位数,并以此作为枢轴等等。
但是,要找到每个组的中位数,我们必须先对组进行排序。视频说使用插入排序或归并排序,但那些算法不是O(nlogn)吗?那么,如果排序已经花费 O(nlogn),整个 运行ning 时间怎么可能是 O(n)?
编辑前我错了
我读过 n/5 组元素,而不是 n/5 组 5 个元素。
排序 N/5 个包含 5 个或更少元素的集合是 O(N/5 * 5*log(5))
,这在复杂时间上是线性的。
the set of numbers or array into n/5 groups of 5 elements and the .... Then we find the median of each group... However to find the median of each group we have to sort the groups first. The video says use insertion sort or merge sort, but aren't those algorithms O(nlogn)? So how could the overall running time be O(n) if sorting already takes O(nlogn)?
不完全正确。由于对 5 人一组进行排序只需 5log(5)
,而我们用了 n
时间就完成了。那将是 n*5/5 log(5) = nlog(5)
。那仍然是线性的。
我最近观看了一段视频,内容是关于如何在 O(n) 时间内制作选择算法 运行,我对制作算法过程中的一个步骤感到困惑。
视频说我们应该将数字集或数组分成 n/5 组,每组 5 个元素,其余元素在另一组中。然后我们找到每组的中位数。然后我们找到中位数的中位数,并以此作为枢轴等等。
但是,要找到每个组的中位数,我们必须先对组进行排序。视频说使用插入排序或归并排序,但那些算法不是O(nlogn)吗?那么,如果排序已经花费 O(nlogn),整个 运行ning 时间怎么可能是 O(n)?
编辑前我错了
我读过 n/5 组元素,而不是 n/5 组 5 个元素。
排序 N/5 个包含 5 个或更少元素的集合是 O(N/5 * 5*log(5))
,这在复杂时间上是线性的。
the set of numbers or array into n/5 groups of 5 elements and the .... Then we find the median of each group... However to find the median of each group we have to sort the groups first. The video says use insertion sort or merge sort, but aren't those algorithms O(nlogn)? So how could the overall running time be O(n) if sorting already takes O(nlogn)?
不完全正确。由于对 5 人一组进行排序只需 5log(5)
,而我们用了 n
时间就完成了。那将是 n*5/5 log(5) = nlog(5)
。那仍然是线性的。