From 37b252697da4638bebc26531f7859314bfbc2ba7 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Sun, 23 Aug 2020 00:29:00 +0530 Subject: [PATCH] test added --- search/median_search.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index f252e843..08cb676c 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -4,7 +4,7 @@ * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) * * @details - * 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. + * Given an array A[1,...,n] of n numbers and an index idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. * median_of_medians(A, i): * #divide A into sublists of len 5 * sublists = [A[j:j+5] for j in range(0, len(A), 5)] @@ -41,6 +41,7 @@ #include #include #include +#include /** * @namespace search @@ -100,11 +101,33 @@ int median_of_medians(const std::vector& A, const int& idx) { } // namespace median_search } // namespace search +/** + * Function to test above algorithm + */ +void test(){ + std::vector A{25,21,98,100,76,22,43,60,89,87}; + int i = 3; + assert(A[6] == search::median_search::median_of_medians(A, i)); // A[6] = 43, is the fourth smallest element. + std::cout << "test case:1 passed\n"; + + std::vector B{1,2,3,4,5,6}; + int j = 4; + assert(B[4] == search::median_search::median_of_medians(B, j)); // B[4] = 5, is the fifth smallest element. + std::cout << "test case:2 passed\n"; + + std::vector C{1,2,3,4,5,1000,8,9,99}; + int k = 3; + assert(C[3] == search::median_search::median_of_medians(C, k)); // C[3] = 4, is the fourth smallest element. + std::cout << "test case:3 passed\n"; + std::cout << "--All tests passed--\n"; +} + /** * Main function */ int main() { + test(); int n = 0; std::cout << "Enter Size of Array: "; std::cin >> n; -- GitLab