mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Suggested changes has been made.
This commit is contained in:
parent
a1242904e1
commit
b50591dfce
@ -4,7 +4,7 @@
|
|||||||
* @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/)
|
* @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/)
|
||||||
*
|
*
|
||||||
* @details
|
* @details
|
||||||
* Given an array A[1,...,n] of n numbers and an index idx, where 1≤idx≤ n, find the i-th smallest element of A.
|
* Given an array A[1,...,n] of n numbers and an index i, where 1 ≤ i ≤ n, find the i-th smallest element of A.
|
||||||
* median_of_medians(A, i):
|
* median_of_medians(A, i):
|
||||||
* #divide A into sublists of len 5
|
* #divide A into sublists of len 5
|
||||||
* sublists = [A[j:j+5] for j in range(0, len(A), 5)]
|
* sublists = [A[j:j+5] for j in range(0, len(A), 5)]
|
||||||
@ -107,12 +107,12 @@ int median_of_medians(const std::vector<int>& A, const int& idx) {
|
|||||||
void test(){
|
void test(){
|
||||||
std::vector<int> A{25,21,98,100,76,22,43,60,89,87};
|
std::vector<int> A{25,21,98,100,76,22,43,60,89,87};
|
||||||
int i = 3;
|
int i = 3;
|
||||||
assert(A[6] == search::median_search::median_of_medians(A, i)); // A[6] = 43, is the fourth smallest element.
|
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::cout << "test case:1 passed\n";
|
||||||
|
|
||||||
std::vector<int> B{1,2,3,4,5,6};
|
std::vector<int> B{1,2,3,4,5,6};
|
||||||
int j = 4;
|
int j = 4;
|
||||||
assert(B[4] == search::median_search::median_of_medians(B, j)); // B[4] = 5, is the fifth smallest element.
|
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::cout << "test case:2 passed\n";
|
||||||
|
|
||||||
std::vector<int> C{1,2,3,4,5,1000,8,9,99};
|
std::vector<int> C{1,2,3,4,5,1000,8,9,99};
|
||||||
|
Loading…
Reference in New Issue
Block a user