-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.cpp
30 lines (26 loc) · 1009 Bytes
/
quicksort.cpp
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
#include "quicksort.hpp"
void QuickSort::sort( std::vector< LabRat >& rats, int first, int last, bool reversed ) {
if ( first < last ) {
int n = qs_split( rats, first, last, reversed );
sort( rats, first, n-1, reversed );
sort( rats, n+1, last, reversed );
}
}
int QuickSort::qs_split( std::vector< LabRat >& rats, int first, int last, bool reversed ) {
int refIndex = first + rand()%(last - first);
double refValue = rats[refIndex].genes() -> propability;
qs_swap( rats, refIndex, last );
int position = first;
for ( int i = first; i <= last-1; i++ )
if( reversed ? (rats[i].genes()->propability > refValue) : (rats[i].genes()->propability < refValue) ) {
qs_swap( rats, i, position );
position++;
}
qs_swap( rats, position, last );
return position;
}
void QuickSort::qs_swap( std::vector< LabRat >& rats, int id1, int id2 ) {
LabRat tmp = rats[id1];
rats[id1] = rats[id2];
rats[id2] = tmp;
}