From ecddfd2704b156886e159bd92c619eb53a0f130b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 21:30:42 -0400 Subject: [PATCH] use pointers instead --- search/median_search.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 2134a0dbe..7379cad26 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -14,16 +14,16 @@ * @todo add documentation */ template -void comp(X x, std::vector &s1, std::vector &s2, - std::vector &s3) { - if (s1.size() >= x && s1.size() + s2.size() < x) { - std::cout << s2[0] << " is the " << x + 1 << "th element from front"; - } else if (s1.size() > x) { - std::sort(s1.begin(), s1.end()); - std::cout << s1[x] << " is the " << x + 1 << "th element from front"; - } else if (s1.size() + s2.size() <= x && s3.size() > x) { - std::sort(s3.begin(), s3.end()); - std::cout << s3[x - s1.size() - s2.size()] << " is the " << x + 1 +void comp(X x, std::vector *s1, std::vector *s2, + std::vector *s3) { + if (s1->size() >= x && s1->size() + s2->size() < x) { + std::cout << (*s2)[0] << " is the " << x + 1 << "th element from front"; + } else if (s1->size() > x) { + std::sort(s1->begin(), s1->end()); + std::cout << (*s1)[x] << " is the " << x + 1 << "th element from front"; + } else if (s1->size() + s2->size() <= x && s3->size() > x) { + std::sort(s3->begin(), s3->end()); + std::cout << (*s3)[x - s1->size() - s2->size()] << " is the " << x + 1 << "th element from front"; } else { std::cout << x + 1 << " is invalid location"; @@ -71,7 +71,7 @@ int main() { int x; std::cout << "enter the no. to be searched form begining:- "; std::cin >> x; - comp(x - 1, s1, s2, s3); + comp(x - 1, &s1, &s2, &s3); return 0; }