Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions search/median_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,19 @@ int median_of_medians(const std::vector<int>& A, const int& idx) {
m.push_back(a[mid]);
}
int sz = int(m.size());

if(sz <= 5){
std::sort(m.begin(), m.end());
if (m.empty()) {
std::cerr << "Error: Median vector is empty.\n";
exit(1);
}
pivot = m[(sz- 1) / 2];
}
else{
pivot = median_of_medians(m, idx);
}
std::vector<int> low;
pivot = median_of_medians(m, m.size() / 2);
}
std::vector<int> low;
std::vector<int> high;
for(int i = 0; i < r; i++){
if(a[i] < pivot){
Expand Down Expand Up @@ -131,7 +136,11 @@ int main()
int n = 0;
std::cout << "Enter Size of Array: ";
std::cin >> n;
std::vector<int> a(n);
if (n <= 0) {
std::cerr << "Error: Array size must be a positive integer.\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this throw an error instead?

return 1;
}
std::vector<int> a(n);
std::cout << "Enter Array: ";
for(int i = 0; i < n; i++){
std::cin >> a[i];
Expand Down