Modify the Bubblesort algorithm below so that it can handlemissing values - take an integer vector containing NAs and returnan ordered vector with NAs at the end of the ordered vector.
# body of the C ++ bubblesort function# stored in an R character stringbody_bubblesort2 <- ' IntegerVector xx = clone( x ); // use of clone () int n = xx.size(); // no . ofelements int temp; // temporary storageof swap value for( int k = 1; k <= n-1; k ++ ){ // for pass k // loop over pairs ofelements for( int i = 0; i < n-1;i++ ){ if( xx[ i] > xx[ i +1 ] ){ temp = xx[ i + 1 ]; xx[ i + 1 ] = xx[ i ]; xx[ i ] = temp; } // end of if } // end ofloop over array pairs } // end of loop overpasses return( wrap( xx ));'
# compile , link , loadbubblesort2 <- cxxfunction( signature( x = "integer" ), body = body_bubblesort2, plugin = "Rcpp")
# create an R integer vector to input to bubblesortx2 <- as.integer( sample(1:100, size = 100, replace = FALSE ))
# callbubblesort2( x2 ) # returns sorted x2x2 # original x2 is not sorted
Modify the Bubblesort algorithm below so that it can handle missing values - take an integer vector containing NAs and r
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am