64 std::vector<int> a(A.begin(), A.end());
67 for(
int i = 0; i < r; i += 5){
68 std::sort(a.begin() + i, a.begin() + std::min(r, i + 5));
69 int mid = (i + std::min(r, i + 5)) / 2;
72 int sz = int(m.size());
74 std::sort(m.begin(), m.end());
75 pivot = m[(sz- 1) / 2];
81 std::vector<int> high;
82 for(
int i = 0; i < r; i++){
86 else if(a[i] > pivot){
90 int k = int(low.size());
108 std::vector<int> A{25,21,98,100,76,22,43,60,89,87};
110 assert(A[6] == search::median_search::median_of_medians(A, i));
111 std::cout <<
"test case:1 passed\n";
113 std::vector<int> B{1,2,3,4,5,6};
115 assert(B[4] == search::median_search::median_of_medians(B, j));
116 std::cout <<
"test case:2 passed\n";
118 std::vector<int> C{1,2,3,4,5,1000,8,9,99};
120 assert(C[3] == search::median_search::median_of_medians(C, k));
121 std::cout <<
"test case:3 passed\n";
122 std::cout <<
"--All tests passed--\n";
132 std::cout <<
"Enter Size of Array: ";
134 std::vector<int> a(n);
135 std::cout <<
"Enter Array: ";
136 for(
int i = 0; i < n; i++){
139 std::cout <<
"Median: ";
140 int x = search::median_search::median_of_medians(a, (n - 1) / 2);
142 int y = search::median_search::median_of_medians(a, n / 2);
143 std::cout << (float(x) + float(y))/2.0;
148 std::cout <<
"\nTo find i-th smallest element ";
149 std::cout <<
"\nEnter i: ";
153 std::cout << idx + 1<<
"-th smallest element: " << search::median_search::median_of_medians(a, idx) <<
'\n';