mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-28 15:06:38 +08:00
feat: add new Median Search implementation (#1992)
* feat: add new median search algorithm with linked list * Removed filename Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * fix: Requested changes made * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Added algorithm and implementation explanations. Also added wikipedia link. * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Added @brief and wikipedia link to algo * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * moved includes to top of file * fix: clang-tidy fixes applied * fix: clang-tidy fixes * updating DIRECTORY.md * clang-format and clang-tidy fixes forb24ca86e* Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for247e0616* fix: fixed test case failing * clang-format and clang-tidy fixes for4c1400db* fix: test cases now pass * clang-format and clang-tidy fixes forf3027971* Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update search/median_search2.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes forb8b5f5fd* clang-format and clang-tidy fixes ford67d450cCo-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
@@ -7,8 +7,8 @@
|
||||
* @author [Ritika Mukherjee](https://github.com/ritikaa17)
|
||||
*/
|
||||
|
||||
#include <iostream> /// for IO operations
|
||||
#include <cassert> /// for assert
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
* \brief [Algorithm implementation for linear search]
|
||||
@@ -18,17 +18,15 @@
|
||||
* \returns index where the key-value occurs in the array
|
||||
* \returns -1 if key-value not found
|
||||
*/
|
||||
int LinearSearch(int *array, int size, int key)
|
||||
{
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
if (array[i] == key)
|
||||
{
|
||||
int LinearSearch(int *array, int size, int key) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (array[i] == key) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
/* We reach here only in case element is not present in array, return an invalid entry in that case*/
|
||||
/* We reach here only in case element is not present in array, return an
|
||||
* invalid entry in that case*/
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -36,12 +34,10 @@ int LinearSearch(int *array, int size, int key)
|
||||
* @brief Self-test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void tests()
|
||||
{
|
||||
static void tests() {
|
||||
int size = 4;
|
||||
int *array = new int[size];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
array[i] = i;
|
||||
}
|
||||
|
||||
@@ -50,8 +46,7 @@ static void tests()
|
||||
assert(LinearSearch(array, size, 2) == 2);
|
||||
|
||||
size = 6;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
array[i] = i;
|
||||
}
|
||||
|
||||
@@ -60,15 +55,14 @@ static void tests()
|
||||
assert(LinearSearch(array, size, 5) == 5);
|
||||
|
||||
std::cout << "All tests have successfully passed!\n";
|
||||
delete[] array; // free memory up
|
||||
delete[] array; // free memory up
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int mode = 0;
|
||||
|
||||
std::cout << "Choose mode\n";
|
||||
@@ -76,13 +70,12 @@ int main()
|
||||
|
||||
std::cin >> mode;
|
||||
|
||||
if (mode == 2)
|
||||
{
|
||||
if (mode == 2) {
|
||||
int size = 0;
|
||||
std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
|
||||
std::cin >> size;
|
||||
|
||||
while (size <= 0 || size > 30){
|
||||
while (size <= 0 || size > 30) {
|
||||
std::cout << "Size can only be 1-30. Please choose another value: ";
|
||||
std::cin >> size;
|
||||
}
|
||||
@@ -92,8 +85,7 @@ int main()
|
||||
|
||||
// Input for the array elements
|
||||
std::cout << "Enter the array of " << size << " numbers: ";
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
std::cin >> array[i];
|
||||
}
|
||||
|
||||
@@ -101,19 +93,14 @@ int main()
|
||||
std::cin >> key;
|
||||
|
||||
int index = LinearSearch(array, size, key);
|
||||
if (index != -1)
|
||||
{
|
||||
if (index != -1) {
|
||||
std::cout << "Number found at index: " << index << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << "Array element not found";
|
||||
}
|
||||
delete[] array;
|
||||
}
|
||||
else
|
||||
{
|
||||
tests(); // run self-test implementations
|
||||
} else {
|
||||
tests(); // run self-test implementations
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user