私は、実行時エラーを持っているquickselectionquickselectアルゴリズム
#include <cstdlib>
#include <iostream>
using namespace std;
const int n = 15;
int b[100];
int btotal=0;
int c[100];
int ctotal=0;
void read(int a[],int size){
for (int i=1; i<=15; i++) cin >> a[i];
}
int quickselect(int a[], int k)
{
int r = 1 + rand()%(n-1);
int pivot = a[r];
for (int i=1; i<=n; i++) {
if (a[i] < pivot) {
b[i] = a[i];
++btotal;
}
if (a[i] > pivot) {
c[i] = a[i];
++ctotal;
}
}
if (k <= btotal) quickselect(b,k);
if (k > (n-ctotal)) return quickselect(c, k - (n-ctotal));
return pivot;
}
int main(int argc, char *argv[])
{
int a[n];
cout <<" enter contents of array " << endl;
read(a, n);
quickselect(a, 6);
//system("PAUSE");
//return EXIT_SUCCESS;
return 0;
}
1 4 2 3 5 7 6 9 8 10 13 12 11 15 14
次のコードを実行したときに、私はここでそれがインデックスに関連する問題ですが、どこ見つけることができないと思う、私を助けてください
が必要? –
no quickselectはありませんが、私はこのコードの主な問題を推測しました –
クイックソートのピボット値を選択すると、3の中央値を選択する方が良いでしょう。これは、アルゴリズムlikleyが良いピボット値を選択するようになります。ランダムな要素を選択し、その要素が最低値または最高値になった場合、アルゴリズムは非常に遅くなります。 –