-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting.cpp
More file actions
50 lines (39 loc) · 806 Bytes
/
counting.cpp
File metadata and controls
50 lines (39 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// counting swaps and comparisons of different swaps
#include <Rcpp.h>
using namespace Rcpp;
class Special{
public:
int nswap;
int ncomp;
bool Less(int a, int b){
ncomp++;
return a < b;
}
void Swap(NumericVector arr, int i, int j){
nswap++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Special(){
nswap = 0;
ncomp = 0;
}
};
// [[Rcpp::export]]
NumericVector bubble_sort(NumericVector arr) {
int len = arr.size();
Special special;
for(int i = 0; special.Less(i, len); i++) {
for(int j = i+1; special.Less(j, len); j++)
{
if(special.Less(arr[j], arr[i])) {
special.Swap(arr, j , i);
}
}
}
NumericVector res(2);
res[0] = special.nswap;
res[1] = special.ncomp;
return res;
}