-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort_wc.cpp
More file actions
47 lines (38 loc) · 789 Bytes
/
quick_sort_wc.cpp
File metadata and controls
47 lines (38 loc) · 789 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
#include <Rcpp.h>
using namespace Rcpp;
int partition(NumericVector a,int start,int end)
{
int pivot = a[end];
int P_index = start;
int i;
for(i=start;i<end;i++)
{
//Rcout << a[i] << " " << pivot << "\n";
if(a[i] < pivot)
{
//special.Swap(a, i, P_index);
std::swap(a[i], a[P_index]);
P_index++;
}
}
//special.Swap(a, end, P_index);
std::swap(a[end], a[P_index]);
return P_index;
}
void qsort(NumericVector a,int start,int end)
{
if(start < end)
{
int P_index = partition(a, start, end);
//Rcout << a << "\n";
qsort(a, start,P_index-1);
qsort(a, P_index+1,end);
}
}
// [[Rcpp::export]]
NumericVector QuickSortL_WC(NumericVector arr)
{
int len = arr.size();
qsort(arr, 0, len-1);
return 1;
}