[R] building a quicksort function in rcpp
Martin Tully
tulls4472 at gmail.com
Fri Jul 24 17:22:54 CEST 2015
Hi I am using RCPP to build a C++ function for quicksort called qsort.
This function is compiled and loaded through the cxxfunction in R
I am getting the message in R error: no matching function for call to
'qsort(int*&)' The code is below.
It will not run for me and I was wondering if you could help?
library(Rcpp)
library(inline)
incl <- 'int qsort(int xx[], int left, int right) {
int i = left,
j = right;
int tmp;
int pivot = xx[(left + right) / 2];
/* partition */
while (i <= j) {
while (xx[i] < pivot)
i++;
while (xx[j] > pivot)
j--;
if (i <= j) {
tmp = xx[i];
xx[i] = xx[j];
xx[j] = tmp;
i++;
j--;
}
}
/* recursion */
if (left < j){
qsort(xx, left, j);
}
if (i < right){
qsort(xx, i, right);
}
return (qsort(xx));
}
'
sortCpp <- cxxfunction(signature( x = "integer",left = "integer",
right = "integer"),
body = 'IntegerVector arr(x);
int a = as<int>(left);
int b = as<int>(right);
return wrap(qsort(arr));',
include = incl,
plugin = "Rcpp",
verbose = TRUE)
[[alternative HTML version deleted]]
More information about the R-help
mailing list