-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqsort.cpp
More file actions
29 lines (26 loc) · 712 Bytes
/
qsort.cpp
File metadata and controls
29 lines (26 loc) · 712 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
template <typename E, typename Comp>
void qsort(E A[], int i, int j)
{
if (j <= i) return; //don't sort 0 or 1 element
int pivotindex = findpivot(A,i,j);
swap(A, pivotindex,j); //put pivot at end
// k will be the first position in the right subarray
int k = partition<E,Comp>(A, i-1,j, A[j]);
swap(A,k,j);
qsort<E,Comp>(A,i,k-1);
qsort<E,Comp>(A,k+1,j);
}
template <typename E>
inline int findpivot(E A[], int i, int j)
{return (i+j)/2;}
template <typename E, typename Comp>
inline int partition(E A[], int l ,int r, E& pivot)
{
do
{
while (Comp::prior(A[++l],pivot));
while ((l < r) && Comp::prior(pivot,A[--r]));
swap(A, l, r);
} while (l<r);
return 1;
}