* cases from [here](https://brilliant.org/wiki/median-finding-algorithm/)
* \ingroup median search
*/
// Given an array A[1,...,n] of n numbers and an index idx, idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A.
#include <iostream>
#include <algorithm>
#include <vector>
/**
* @namespace search
* @brief Search algorithms
*/
namespacesearch{
/**
* @namespace median_search
* @brief Functions for [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm
* @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/)
*/
namespacemedian_search{
/* Assume that all the elements of A are distinct
def median_of_medians(A, i):
...
...
@@ -43,8 +49,8 @@
* print median_of_medians(B,4) #should be 5
*/
intmedian_of_medians(std::vector<int>a,intidx){
intpivot=0;// initialized with zero
intmedian_of_medians(std::vector<int>a,intidx){// Search the element in **a** whose index is **idx** and return element at index **idx** in **a** (a[idx])
intpivot=0;// initialized with zero
std::vector<int>m;
intr=a.size();
for(inti=0;i<r;i+=5){
...
...
@@ -81,9 +87,12 @@ int median_of_medians(std::vector<int> a, int idx){
returnpivot;
}
}
}// namespace median_search
}// namespace search
/* Main function*/
/**
* Main function
*/
intmain()
{
intn=0;
...
...
@@ -95,9 +104,9 @@ int main()
std::cin>>a[i];
}
std::cout<<"Median: ";// Median defination: https://en.wikipedia.org/wiki/Median