Peter Wolf wrote: sort.6<-function(a){ n<-length(a) adapt<-function(i){i+1} # local function to perform the index correction a<-c(0,a) for(i in 2:n){ j<-i-1 a[adapt(0)]<-a[adapt(i)] while(a[adapt(j)]>a[adapt(0)]){ a[adapt(j+1)]<-a[adapt(j)] j<-j-1 } a[adapt(j+1)]<-a[adapt(0)] } return(a[-1]) }
and Prof Brian Ripley wrote: : since you can shift whole blocks at a time rather than use a while loop. In words the algorithm runs from 2 through length(a) inserting the current element into the subarray to its left, so to follow up Prof Riley's suggestion to replace the while loop with code that copies whole blocks at a time we have the following which does not seem to suffer from lack of 0-origin and is clearer than the double loop approach: sort.6a <- function(a) { for(i in 2:length(a)){ # insert a[i] into subvector to left of it left <- a[1:(i-1)] sel <- left < a[i] a[1:i] <- c( left[sel], a[i], left[!sel] ) } a } ______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html