Skip to content

Instantly share code, notes, and snippets.

@wckdouglas
Last active August 29, 2015 14:21
Show Gist options
  • Save wckdouglas/ccdf7de8a8300c09fba7 to your computer and use it in GitHub Desktop.
Save wckdouglas/ccdf7de8a8300c09fba7 to your computer and use it in GitHub Desktop.
bubble sort
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector bubbleSort(NumericVector a){
//sorting a numeric vector in to a ascending list//
int j, flag, vectorSize = a.size();
double tempVal;
NumericVector sortedVector(clone(a));
NumericVector finalIndex(vectorSize);
while(true) {
flag = 0;
for (j = 0 ; j < (vectorSize - 1); j++){
if (sortedVector[j+1] < sortedVector[j]){
flag ++ ;
tempVal = sortedVector[j];
sortedVector[j] = sortedVector[j+1];
sortedVector[j+1] = tempVal;
}
}
if (flag == 0)
break;
}
return(sortedVector);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment