This commit is contained in:
incredibleLeitman 2020-10-18 12:10:12 +02:00
commit f5971e2cb6
2 changed files with 85 additions and 116 deletions

View File

@ -1,136 +1,104 @@
#pragma once #pragma once
// https://en.wikipedia.org/wiki/Median_of_medians // Implemented pseudocode from https://en.wikipedia.org/wiki/Median_of_medians
// https://oneraynyday.github.io/algorithms/2016/06/17/Median-Of-Medians/
// https://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array-set-3-worst-case-linear-time/
uint32_t findMedian(std::vector<uint32_t> values) uint32_t pivot(std::vector<uint32_t> &v, uint32_t left, uint32_t right);
{ uint32_t partition(std::vector<uint32_t> &v, uint32_t left, uint32_t right, uint32_t pivotIndex, uint32_t n);
return values[(values.size() / 2)];
// Return the index of an element which is close to (but likely not exactly) the median.
uint32_t findMedianOfMedians(std::vector<uint32_t> &v, uint32_t left, uint32_t right, uint32_t n) {
while (true) {
if (left == right) {
return left;
}
uint32_t pivotIndex = pivot(v, left, right);
pivotIndex = partition(v, left, right, pivotIndex, n);
if (n == pivotIndex) {
return n;
} else if (n < pivotIndex) {
right = pivotIndex - 1;
} else {
left = pivotIndex + 1;
}
}
} }
uint32_t findMedianOfMedians(std::vector<std::vector<uint32_t> > values) 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];
std::vector<uint32_t> medians;
for (size_t i = 0; i < values.size(); i++) { std::swap(v[pivotIndex], v[right]);
uint32_t m = findMedian(values[i]);
medians.push_back(m); uint32_t storeIndex = left;
// Move all elements smaller than the pivot to the left of the pivot
for (uint32_t i = left; i < right; i++) {
if (v[i] < pivotValue) {
std::swap(v[storeIndex], v[i]);
storeIndex++;
} }
return findMedian(medians); }
// Move all elements equal to the pivot right after
// the smaller elements
uint32_t storeIndexEq = storeIndex;
for (uint32_t i = storeIndex; i < right; i++) {
if (v[i] == pivotValue) {
std::swap(v[storeIndexEq], v[i]);
storeIndexEq++;
}
}
std::swap(v[right], v[storeIndexEq]);
if (n < storeIndex) {
return storeIndex;
}
if (n <= storeIndexEq) {
return n;
}
return storeIndexEq;
} }
uint32_t getMedianOfMedians(const std::vector<uint32_t> values, uint32_t k) uint32_t partition5(std::vector<uint32_t> &v, uint32_t left, uint32_t right) {
{ uint32_t i = left + 1;
// Divide the list into n/5 lists of 5 elements each
std::vector<std::vector<uint32_t> > vec2D; while (i <= right) {
size_t count = 0; uint32_t j = i;
while (count != values.size()) {
size_t countRow = 0; while (j > left && v[j - 1] > v[j]) {
std::vector<uint32_t> row; std::swap(v[j - 1], v[j]);
while ((countRow < 5) && (count < values.size())) j = j - 1;
{
row.push_back(values[count]);
count++;
countRow++;
}
vec2D.push_back(row);
} }
// Calculating a new pivot for making splits i = i + 1;
uint32_t m = findMedianOfMedians(vec2D);
// Partition the list into unique elements larger than 'm' (call this sublist L1) and those smaller them 'm' (call this sublist L2)
std::vector<uint32_t> L1, L2;
for (size_t i = 0; i < vec2D.size(); i++)
{
for (size_t j = 0; j < vec2D[i].size(); j++)
{
if (vec2D[i][j] > m)
{
L1.push_back(vec2D[i][j]);
}
else if (vec2D[i][j] < m)
{
L2.push_back(vec2D[i][j]);
}
}
} }
if (k <= L1.size()) return (left + right) / 2;
{
return getMedianOfMedians(L1, k);
}
else if (k > (L1.size() + 1))
{
return getMedianOfMedians(L2, k - ((int)L1.size()) - 1);
}
return m;
} }
// A simple function to find median of arr[]. uint32_t pivot(std::vector<uint32_t> &v, uint32_t left, uint32_t right) {
// This is called only for an array of size 5 in this program. // for 5 or less elements just get median
uint32_t findMedian(uint32_t arr[], int n) if (right - left < 5) {
{ return partition5(v, left, right);
std::sort(arr, arr + n); // Sort the array
return arr[n / 2]; // Return middle element
}
// searches for x in arr[l..r], and partitions the array around x
int partition(uint32_t arr[], int l, int r, uint32_t pivotValue)
{
// Search for x in arr[l..r] and move it to end
int i;
for (i = l; i < r; i++)
if (arr[i] == pivotValue)
break;
swap(&arr[i], &arr[r]);
// Standard partition algorithm
i = l;
for (int j = l; j < r; j++)
{
if (arr[j] <= pivotValue)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i], &arr[r]);
return i;
}
// Returns k'th smallest element in arr[l..r] in worst case
// linear time. ASSUMPTION: ALL ELEMENTS IN ARR[] ARE DISTINCT
//int getMedianOfMedians(int arr[], int l, int r, int k)
uint32_t getMedianOfMedians(uint32_t* arr, int l, int r, int k)
{
int n = r - l + 1; // Number of elements in arr[l..r]
// Divide arr[] in groups of size 5, calculate median
// of every group and store it in median[] array.
// There will be floor((n + 4) / 5) groups;
//int median[(n + 4) / 5]; // non VS compliant!
uint32_t* median = new uint32_t[(n + 4) / 5];
int i = 0;
for (i = 0; i < n / 5; i++)
median[i] = findMedian(arr + l + i * 5, 5);
if (i * 5 < n) //For last group with less than 5 elements
{
median[i] = findMedian(arr + l + i * 5, n % 5);
i++;
} }
// Find median of all medians using recursive call. // otherwise move the medians of five-element subgroups to the first n/5 positions
// If median[] has only one element, then no need for recursive call for (uint32_t i = left; i <= right; i += 5) {
uint32_t medOfMed = (i == 1) ? median[0] : getMedianOfMedians(median, 0, i - 1, i / 2); // get the median position of the i'th five-element subgroup
uint32_t subRight = i + 4;
// Partition the array around a random element and if (subRight > right) {
// get position of pivot element in sorted array subRight = right;
int pos = partition(arr, l, r, medOfMed); }
if (pos - l == k - 1) return arr[pos]; uint32_t median5 = partition5(v, i, subRight);
else if (pos - l > k - 1) std::swap(v[median5], v[left + (i - left) / 5]);
return getMedianOfMedians(arr, l, pos - 1, k); }
else return getMedianOfMedians(arr, pos + 1, r, k - pos + l - 1);
// compute the median of the n/5 medians-of-five
uint32_t mid = (right - left) / 10 + left + 1;
return findMedianOfMedians(v, left, left + (right - left) / 5, mid);
} }

View File

@ -65,8 +65,9 @@ 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 << getMedianOfMedians(numbers, idxMed + 1) << std::endl; std::cout << type << mom_numbers[findMedianOfMedians(mom_numbers, 0, numbers.size() - 1, idxMed + 1)] << std::endl;
Timing::getInstance()->stopRecord(type); Timing::getInstance()->stopRecord(type);
// ein weiterer Median - Algorithmus aus der Literatur - realized with array // ein weiterer Median - Algorithmus aus der Literatur - realized with array