c语言中位数的算法,c – 中位数中位数算法误解?

我已经明白了

我知道中位数算法的中位数(我将表示为MoM)是一个高常数因子O(N)算法.它找到k组的中位数(通常为5)并将它们用作下一个迭代的集合来查找中位数.找到它之后的枢轴将在原始集合的3 / 10n和7 / 10n之间,其中n是找到一个中间基本情况所花费的迭代次数.

当我为MoM运行此代码时,我不断遇到分段错误,但我不确定原因.我调试了它,并认为问题在于我正在调用medianOfMedian(medians,0,medians.size() – 1,medians.size()/ 2);.但是,我认为这在逻辑上是合理的,因为我们应该通过调用自己来递归地找到中位数.也许我的基础案例不正确?在YogiBearian在youtube(斯坦福教授,链接:https://www.youtube.com/watch?v=YU1HfMiJzwg)的教程中,他没有说明任何额外的基础案例来处理MoM中递归的O(N / 5)操作.

完整代码

注意:根据建议,我添加了一个基本案例,并使用矢量使用.at()函数.

static const int GROUP_SIZE = 5;

/* Helper function for m of m. This function divides the array into chunks of 5

* and finds the median of each group and puts it into a vector to return.

* The last group will be sorted and the median will be found despite its uneven size.

*/

vector findMedians(vector& vec, int start, int end){

vector medians;

for(int i = start; i <= end; i+= GROUP_SIZE){

std::sort(vec.begin()+i, min(vec.begin()+i+GROUP_SIZE, vec.end()));

medians.push_back(vec.at(min(i + (GROUP_SIZE/2), (i + end)/2)));

}

return medians;

}

/* Job is to partition the array into chunks of 5(subject to change via const)

* And then find the median of them. Do this recursively using select as well.

*/

int medianOfMedian(vector& vec, int start, int end, int k){

/* Acquire the medians of the 5-groups */

vector medians = findMedians(vec, start, end);

/* Find the median of this */

int pivotVal;

if(medians.size() == 1)

pivotVal = medians.at(0);

else

pivotVal = medianOfMedian(medians, 0, medians.size()-1, medians.size()/2);

/* Stealing a page from select() ... */

int pivot = partitionHelper(vec, pivotVal, start, end);

cout << "After pivoting with the value " << pivot << " we get : " << endl;

for(int i = start; i < end; i++){

cout << vec.at(i) << ", ";

}

cout << "\n\n" << endl;

usleep(10000);

int length = pivot - start + 1;

if(k < length){

return medianOfMedian(vec, k, start, pivot-1);

}

else if(k == length){

return vec[k];

}

else{

return medianOfMedian(vec, k-length, pivot+1, end);

}

}

一些额外的功能,以帮助单元测试

以下是我为这两个函数编写的一些单元测试.希望他们有所帮助.

vector initialize(int size, int mod){

int arr[size];

for(int i = 0; i < size; i++){

arr[i] = rand() % mod;

}

vector vec(arr, arr+size);

return vec;

}

/* Unit test for findMedians */

void testFindMedians(){

const int SIZE = 36;

const int MOD = 20;

vector vec = initialize(SIZE, MOD);

for(int i = 0; i < SIZE; i++){

cout << vec[i] << ", ";

}

cout << "\n\n" << endl;

vector medians = findMedians(vec, 0, SIZE-1);

cout << "The 5-sorted version: " << endl;

for(int i = 0; i < SIZE; i++){

cout << vec[i] << ", ";

}

cout << "\n\n" << endl;

cout << "The medians extracted: " << endl;

for(int i = 0; i < medians.size(); i++){

cout << medians[i] << ", ";

}

cout << "\n\n" << endl;

}

/* Unit test for medianOfMedian */

void testMedianOfMedian(){

const int SIZE = 30;

const int MOD = 70;

vector vec = initialize(SIZE, MOD);

cout << "Given array : " << endl;

for(int i = 0; i < SIZE; i++){

cout << vec[i] << ", ";

}

cout << "\n\n" << endl;

int median = medianOfMedian(vec, 0, vec.size()-1, vec.size()/2);

cout << "\n\nThe median is : " << median << endl;

cout << "As opposed to sorting and then showing the median... : " << endl;

std::sort(vec.begin(), vec.end());

cout << "sorted array : " << endl;

for(int i = 0; i < SIZE; i++){

if(i == SIZE/2)

cout << "**";

cout << vec[i] << ", ";

}

cout << "Median : " << vec[SIZE/2] << endl;

}

关于我得到的输出的额外部分

Given array :

7, 49, 23, 48, 20, 62, 44, 8, 43, 29, 20, 65, 42, 62, 7, 33, 37, 39, 60, 52, 53, 19, 29, 7, 50, 3, 69, 58, 56, 65,

After pivoting with the value 5 we get :

23, 29, 39, 42, 43,

After pivoting with the value 0 we get :

39,

Segmentation Fault: 11

在分段错误之前,似乎没问题和花花公子.我有信心我的分区功能也是如此(是leetcode问题的实现之一).

免责声明:这不是一个家庭作业问题,而是我在leetcode问题集中使用quickSelect后对算法的好奇心.

如果我提出的问题需要更多细化MVCE,请告诉我,谢谢!

编辑:我发现我的代码中的递归分区方案是错误的.正如Pradhan指出的那样 – 我不知何故有空向量导致开始和结束分别为0和-1,导致我从调用它的无限循环中产生分段错误.仍然试图弄清楚这一部分.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值