c++ - 基于修改后的quick_sort的nth_element实现,未按预期工作

标签 c++ algorithm quicksort nth-element

为了理解 quick_sort ,我正在尝试实现 nth_element
作为测试的基本事实,我正在使用std::nth_element我的算法在很多输入上都失败了,例如a = {6,1,7,5,3,8,2,4,9}
如何解决?

std::random_device dev;
std::mt19937 rng(dev());

int kth_element(vector<long long> a, int l, int r, int k) {
  if(l >= r) return a[l];
  int i = l, j = r;
  uniform_int_distribution<int> dist(l,r);
  int X = a[dist(rng)];
  while(i < j) {
    while(a[i] < X) ++i;
    while(a[j] > X) --j;
    if(i < j) {
      swap(a[i], a[j]);
      ++i;
      --j;
    }
  }
  if(k >= l && k < i)
    return kth_element(a, l,i-1,k);
  else if (j < k && k <= r)
    return kth_element(a,j+1,r,k);
  return X;
}

int kth_element(vector<long long> a, int k) {
  int n = a.size();
  return kth_element(a,0,n-1,k);
}

int main(int argc, char* argv[]) {
  vector<long long> a = {1,2,3,4,5,6,7,8,9};
  int n = a.size();
  for(int i = 0; i < n; ++i)
    cout << kth_element(a,i) << '\n';

  random_device rd;
  mt19937 rng(rd());
  shuffle(a.begin(), a.end(), rng);
  show_vars(a);
  for(int i = 0; i < n; ++i) {
    cout << i << ": " << kth_element(a,i);
    nth_element(a.begin(), a.begin()+i,a.end());
    cout << ", " << a[i] << '\n';
    assert(kth_element(a,i) == a[i]);
  }

  return 0;
}

如果我在循环中进行测试,请更新,该算法将失败:
int main(int argc, char* argv[]) {
  vector<long long> a = {1,2,3,4,5,6,7,8,9};
  int n = a.size();
  // for(int i = 0; i < n; ++i)
  //   cout << kth_element(a,i) << '\n';

  random_device rd;
  mt19937 rng(rd());
  for(int t = 0; t < 1000; ++t) {
    shuffle(a.begin(), a.end(), rng);
    // show_vars(a);
    for(int i = 0; i < n; ++i) {
      // show_vars(i);
      long long kth = kth_element(a,i);
      cout << i << ": " << kth;
      nth_element(a.begin(), a.begin()+i,a.end());
      cout << ", " << a[i] << '\n';
      // show_vars(kth, a[i]);
      assert(kth == a[i]);
    }
  }

  return 0;
}

最佳答案

如果有人尝试基于Hoar分区实现nth_element:

使用2个指针i,j并保持循环不变:
对于k = 0..i-1 a [k] <= X和对于z = j + 1 .. n-1 a [z]> = X
循环之后,可以通过两种方式对数组进行分区:
... | j |我| ...
要么
... | j | X |我| ...
[L,j] <= X中idx的所有元素(左侧)
在[i,R]> = X中具有idx(右侧)
根据所需路径中左/右部分中元素的数量重复出现

std::random_device dev;
std::mt19937 rng(dev());
long long kth(vector<long long>& a, int L, int R, int k) {
  if(L>=R) return a[L];

  int i = L,j = R;

  std::uniform_int_distribution<std::mt19937::result_type> dist(L,R);
  long long X = a[dist(rng)];

  while(i <= j) {
    while(a[i] < X) ++i;
    while(a[j] > X) --j;
    if(i <= j) {
      swap(a[i], a[j]);
      ++i;
      --j;
    }
  }


  if(k <= j)
    return kth(a, L,j,k);
  if (k >=i)
    return kth(a,i,R,k);
  return X;
}

关于c++ - 基于修改后的quick_sort的nth_element实现,未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61771376/

相关文章:

c++ - 向许多文件添加 namespace 声明?

c++ - 在 C++ 中显示嵌套 typedef 的最基本类型声明

python - 快速排序没有变得更快

c - C 中的递归快速排序

c++ - 打破 C++ 中的嵌套循环

c# - 如何使用一系列键搜索 C# 字典?

algorithm - 投资者和资金池 - 回溯

java - 在java中将大数转换为小数的算法

r - R 中意外的快速排序行为

c++ - 从文本文件中读取矩阵并在 C++ 中使用一列的数字