removed old MoM array implementation; fixed MoM index; using getFunction to internally copy param

This commit is contained in:
incredibleLeitman 2020-10-18 12:21:00 +02:00
parent f5971e2cb6
commit 74d6fabf68
2 changed files with 11 additions and 15 deletions

View File

@ -28,14 +28,14 @@ uint32_t findMedianOfMedians(std::vector<uint32_t> &v, uint32_t left, uint32_t r
uint32_t partition(std::vector<uint32_t> &v, uint32_t left, uint32_t right, uint32_t pivotIndex, uint32_t n) { uint32_t partition(std::vector<uint32_t> &v, uint32_t left, uint32_t right, uint32_t pivotIndex, uint32_t n) {
uint32_t pivotValue = v[pivotIndex]; uint32_t pivotValue = v[pivotIndex];
std::swap(v[pivotIndex], v[right]); swap(&v[pivotIndex], &v[right]);
uint32_t storeIndex = left; uint32_t storeIndex = left;
// Move all elements smaller than the pivot to the left of the pivot // Move all elements smaller than the pivot to the left of the pivot
for (uint32_t i = left; i < right; i++) { for (uint32_t i = left; i < right; i++) {
if (v[i] < pivotValue) { if (v[i] < pivotValue) {
std::swap(v[storeIndex], v[i]); swap(&v[storeIndex], &v[i]);
storeIndex++; storeIndex++;
} }
} }
@ -46,12 +46,12 @@ uint32_t partition(std::vector<uint32_t> &v, uint32_t left, uint32_t right, uint
for (uint32_t i = storeIndex; i < right; i++) { for (uint32_t i = storeIndex; i < right; i++) {
if (v[i] == pivotValue) { if (v[i] == pivotValue) {
std::swap(v[storeIndexEq], v[i]); swap(&v[storeIndexEq], &v[i]);
storeIndexEq++; storeIndexEq++;
} }
} }
std::swap(v[right], v[storeIndexEq]); swap(&v[right], &v[storeIndexEq]);
if (n < storeIndex) { if (n < storeIndex) {
return storeIndex; return storeIndex;
@ -69,7 +69,7 @@ uint32_t partition5(std::vector<uint32_t> &v, uint32_t left, uint32_t right) {
uint32_t j = i; uint32_t j = i;
while (j > left && v[j - 1] > v[j]) { while (j > left && v[j - 1] > v[j]) {
std::swap(v[j - 1], v[j]); swap(&v[j - 1], &v[j]);
j = j - 1; j = j - 1;
} }
@ -95,10 +95,14 @@ uint32_t pivot(std::vector<uint32_t> &v, uint32_t left, uint32_t right) {
} }
uint32_t median5 = partition5(v, i, subRight); uint32_t median5 = partition5(v, i, subRight);
std::swap(v[median5], v[left + (i - left) / 5]); swap(&v[median5], &v[left + (i - left) / 5]);
} }
// compute the median of the n/5 medians-of-five // compute the median of the n/5 medians-of-five
uint32_t mid = (right - left) / 10 + left + 1; uint32_t mid = (right - left) / 10 + left + 1;
return findMedianOfMedians(v, left, left + (right - left) / 5, mid); return findMedianOfMedians(v, left, left + (right - left) / 5, mid);
}
uint32_t getMedianOfMedians(std::vector<uint32_t> v, uint32_t left, uint32_t right, uint32_t n) {
return v[findMedianOfMedians(v, left, right, n)];
} }

View File

@ -65,18 +65,10 @@ int main(int argc, char** argv)
// ein weiterer Median - Algorithmus aus der Literatur - implemented with std::vector // ein weiterer Median - Algorithmus aus der Literatur - implemented with std::vector
type = "vector median of medians\t\t"; type = "vector median of medians\t\t";
std::vector<uint32_t> mom_numbers = std::vector<uint32_t>(numbers);
Timing::getInstance()->startRecord(type); Timing::getInstance()->startRecord(type);
std::cout << type << mom_numbers[findMedianOfMedians(mom_numbers, 0, numbers.size() - 1, idxMed + 1)] << std::endl; std::cout << type << getMedianOfMedians(numbers, 0, numbers.size() - 1, idxMed) << std::endl;
Timing::getInstance()->stopRecord(type); Timing::getInstance()->stopRecord(type);
// ein weiterer Median - Algorithmus aus der Literatur - realized with array
/*type = "array median of medians";
std::copy(numbers.begin(), numbers.end(), array);
Timing::getInstance()->startRecord(type);
std::cout << type << ": \t\t" << getMedianOfMedians(array, 0, numbers.size() - 1, idxMed + 1) << std::endl;
Timing::getInstance()->stopRecord(type);*/
// noch ein ein weiterer Median - Algorithmus weil wir so cool sind // noch ein ein weiterer Median - Algorithmus weil wir so cool sind
type = "wirth kth element\t\t\t"; type = "wirth kth element\t\t\t";
Timing::getInstance()->startRecord(type); Timing::getInstance()->startRecord(type);