gpt4 book ai didi

c++ - quickselect 算法因重复元素而失败

转载 作者:太空狗 更新时间:2023-10-29 21:43:44 25 4
gpt4 key购买 nike

我已经实现了快速选择算法。

我有一个问题,当我在数组中使用重复项时,我的算法会陷入无限循环...

你能帮我让它工作吗?

预期复杂度为 O(n),最坏情况为 O(n^2)?

#include <iostream> 
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int rand_partition(vector<int> &a, int left, int right) {
int pivotIndex = left + (rand() % (right - left));
//int m = left + (right - left) / 2; //... to test the algo...no rand at this point
int pivot = a[pivotIndex];
int i = left;
int j = right;

do {
while (a[i] < pivot) i++; // find left element > pivot
while (a[j] > pivot) j--; // find right element < pivot

// if i and j not already overlapped, we can swap
if (i < j) {
swap(a[i], a[j]);
}
} while (i < j);

return i;
}

// Returns the n-th smallest element of list within left..right inclusive (i.e. n is zero-based).
int quick_select(vector<int> &a, int left, int right, int n) {
if (left == right) { // If the list contains only one element
return a[left]; // Return that element
}

int pivotIndex = rand_partition(a, left, right);

// The pivot is in its final sorted position
if (n == pivotIndex) {
return a[n];
}
else if (n < pivotIndex) {
return quick_select(a, left, pivotIndex - 1, n);
}
else {
return quick_select(a, pivotIndex + 1, right, n);
}
}

int main() {

vector<int> vec= {1, 0, 3, 5, 0, 8, 6, 0, 9, 0};

cout << quick_select(vec, 0, vec.size() - 1, 5) << endl;

return 0;
}

最佳答案

您的代码中存在多个问题。

  • 首先,在函数 quick_select() 中, 你正在比较 pivotIndexn直接地。自 left不总是 0,你应该比较 n左边部分的长度等于pivotIndex - left + 1 .
  • 何时n > length , 你只需调用 quick_select(a, pivotIndex + 1, right, n)递归地,这时,就是整个 vector 的第N个元素在它的右半部分,就是右半部分的第(N - (pivotIndex - left + 1) )-th 个元素 vector 。代码应该是quick_select(a, pivotIndex + 1, right, n - (pivotIndex - left + 1) ) (n 是基于 ONE 的)。
  • 您似乎在使用 Hoare 的分区算法并且实现不正确。即使它有效,当 HOARE-PARTITION 终止时,它返回一个值 j 使得 A[p...j] ≤ A[j+1...r] , 但我们想要 A[p...j-1] ≤ A[j] ≤ A[j+1...r]quick_select() .所以我使用 rand_partition()基于我在 another post 上写的 Lomuto 分区算法

这是固定的 quick_select()它返回 vector 的第 N 个(请注意 n 是基于 ONE 的)最小元素:

int quick_select(vector<int> &a, int left, int right, int n)
{
if ( left == right )
return a[left];
int pivotIndex = partition(a, left, right);

int length = pivotIndex - left + 1;
if ( length == n)
return a[pivotIndex];
else if ( n < length )
return quick_select(a, left, pivotIndex - 1, n);
else
return quick_select(a, pivotIndex + 1, right, n - length);
}

thisrand_partition() :

int rand_partition(vector<int> &arr, int start, int end)
{
int pivot_index = start + rand() % (end - start + 1);
int pivot = arr[pivot_index];

swap(arr[pivot_index], arr[end]); // swap random pivot to end.
pivot_index = end;
int i = start -1;

for(int j = start; j <= end - 1; j++)
{
if(arr[j] <= pivot)
{
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[pivot_index]); // swap back the pivot

return i + 1;
}

调用srand()首先初始化随机数生成器,以便在调用 rand() 时可以获得类似随机数的数字。 .
测试上述功能的驱动程序:

int main()
{
int A1[] = {1, 0, 3, 5, 0, 8, 6, 0, 9, 0};
vector<int> a(A1, A1 + 10);
cout << "6st order element " << quick_select(a, 0, 9, 6) << endl;
vector<int> b(A1, A1 + 10); // note that the vector is modified by quick_select()
cout << "7nd order element " << quick_select(b, 0, 9, 7) << endl;
vector<int> c(A1, A1 + 10);
cout << "8rd order element " << quick_select(c, 0, 9, 8) << endl;
vector<int> d(A1, A1 + 10);
cout << "9th order element " << quick_select(d, 0, 9, 9) << endl;
vector<int> e(A1, A1 + 10);
cout << "10th order element " << quick_select(e, 0, 9, 10) << endl;
}

关于c++ - quickselect 算法因重复元素而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22080055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com