[R] building a quicksort function in rcpp
Uwe Ligges
ligges at statistik.tu-dortmund.de
Sat Jul 25 11:05:05 CEST 2015
Actually sort() is already there ....
Best,
Uwe Ligges
On 24.07.2015 17:22, Martin Tully wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list