mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fixed clang-tidy warnings
This commit is contained in:
parent
1433ef2210
commit
0d61b0494f
@ -8,7 +8,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
/* Assume that all the elements of A are distinct
|
/* Assume that all the elements of A are distinct
|
||||||
def median_of_medians(A, i):
|
def median_of_medians(A, i):
|
||||||
@ -44,67 +43,72 @@ using namespace std;
|
|||||||
* print median_of_medians(B,4) #should be 5
|
* print median_of_medians(B,4) #should be 5
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int median_of_medians(vector<int> a, int idx){
|
int median_of_medians(std::vector<int> a, int idx){
|
||||||
int pivot;
|
int pivot = 0; // initialized with zero
|
||||||
vector<int> m;
|
std::vector<int> m;
|
||||||
int r = a.size();
|
int r = a.size();
|
||||||
for(int i = 0; i < r; i += 5){
|
for(int i = 0; i < r; i += 5){
|
||||||
sort(a.begin() + i, a.begin() + min(r, i + 5));
|
std::sort(a.begin() + i, a.begin() + std::min(r, i + 5));
|
||||||
int mid = (i + min(r, i + 5)) / 2;
|
int mid = (i + std::min(r, i + 5)) / 2;
|
||||||
m.push_back(a[mid]);
|
m.push_back(a[mid]);
|
||||||
}
|
}
|
||||||
int sz = int(m.size());
|
int sz = int(m.size());
|
||||||
if(sz <= 5){
|
if(sz <= 5){
|
||||||
sort(m.begin(), m.end());
|
std::sort(m.begin(), m.end());
|
||||||
pivot = m[(sz- 1) / 2];
|
pivot = m[(sz- 1) / 2];
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
pivot = median_of_medians(m, idx);
|
pivot = median_of_medians(m, idx);
|
||||||
}
|
}
|
||||||
vector<int> low;
|
std::vector<int> low;
|
||||||
vector<int> high;
|
std::vector<int> high;
|
||||||
for(int i = 0; i < r; i++){
|
for(int i = 0; i < r; i++){
|
||||||
if(a[i] < pivot)
|
if(a[i] < pivot){
|
||||||
low.push_back(a[i]);
|
low.push_back(a[i]);
|
||||||
else if(a[i] > pivot)
|
}
|
||||||
|
else if(a[i] > pivot){
|
||||||
high.push_back(a[i]);
|
high.push_back(a[i]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
int k = int(low.size());
|
int k = int(low.size());
|
||||||
if(idx < k)
|
if(idx < k){
|
||||||
return median_of_medians(low, idx);
|
return median_of_medians(low, idx);
|
||||||
else if(idx > k)
|
}
|
||||||
|
else if(idx > k){
|
||||||
return median_of_medians(high, idx-k-1);
|
return median_of_medians(high, idx-k-1);
|
||||||
else
|
}
|
||||||
|
else{
|
||||||
return pivot;
|
return pivot;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Main function*/
|
/* Main function*/
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int n;
|
int n = 0;
|
||||||
cout << "Enter Size of Array: ";
|
std::cout << "Enter Size of Array: ";
|
||||||
cin >> n;
|
std::cin >> n;
|
||||||
vector<int> a(n);
|
std::vector<int> a(n);
|
||||||
cout << "Enter Array: ";
|
std::cout << "Enter Array: ";
|
||||||
for(int i = 0; i < n; i++)
|
for(int i = 0; i < n; i++){
|
||||||
cin >> a[i];
|
std::cin >> a[i];
|
||||||
|
}
|
||||||
cout << "Median: ";
|
std::cout << "Median: "; // Median defination: https://en.wikipedia.org/wiki/Median
|
||||||
int x = median_of_medians(a, (n - 1) / 2);
|
int x = median_of_medians(a, (n - 1) / 2);
|
||||||
if(n % 2 == 0){
|
if(n % 2 == 0){
|
||||||
int y = median_of_medians(a, n / 2);
|
int y = median_of_medians(a, n / 2);
|
||||||
cout << (float(x) + float(y))/2.0;
|
std::cout << (float(x) + float(y))/2.0;
|
||||||
}
|
}
|
||||||
else
|
else{
|
||||||
cout << x;
|
std::cout << x;
|
||||||
|
}
|
||||||
cout << "\nTo find i-th smallest element ";
|
std::cout << "\nTo find i-th smallest element ";
|
||||||
cout << "\nEnter i: ";
|
std::cout << "\nEnter i: ";
|
||||||
int idx;
|
int idx = 0;
|
||||||
cin >> idx;
|
std::cin >> idx;
|
||||||
idx--;
|
idx--;
|
||||||
cout << "i-th smallest element: " << median_of_medians(a, idx) << endl;
|
std::cout << idx + 1<< "-th smallest element: " << median_of_medians(a, idx) << '\n';
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user