Skip to content

Commit

Permalink
Added binary search implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
seancyw committed Jan 24, 2017
1 parent 706b977 commit 9e67c38
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 3 deletions.
81 changes: 81 additions & 0 deletions Algorithms/SearchAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

namespace searchAlgorithms {

Expand Down Expand Up @@ -73,7 +75,86 @@ namespace searchAlgorithms {
return -1;
}

//Search a value in an array
//Return its index
template<typename T>
int binarySearchIndex(std::vector<T> & element, const T& searchValue)
{
if (element.size() == 0) {
throw std::exception("Element size cannot be 0. Cannot perform search");
return -1;
}

//Sort the elements before search
std::sort(element.begin(), element.end());

return binarySearchIndexHelper(element, searchValue, 0, element.size());
}

template<typename T>
int binarySearchIndexHelper(std::vector<T> & element, const T& searchValue, const size_t startIndex, const size_t endIndex)
{
if (startIndex > endIndex || startIndex == endIndex)
return -1;

size_t middle = (startIndex + endIndex) / 2;

if (searchValue == element[middle])
return middle;
else if (searchValue < element[middle])
return binarySearchIndexHelper(element, searchValue, startIndex, middle - 1);
else if (searchValue > element[middle])
return binarySearchIndexHelper(element, searchValue, middle + 1, endIndex);

return -1;
}

//Search a value in an array
//Return its index
template<typename T>
bool binarySearchValue(std::vector<T> & element, const T& searchValue)
{
if (element.size() == 0) {
throw std::exception("Element size cannot be 0. Cannot perform search");
return false;
}

//Sort the elements before search
std::cout << "Sort array before perform searching:\n";

std::sort(element.begin(), element.end());

std::cout << "Array after sort:\n";
printArray(element);

//Peform binary search
return binarySearchValueHelper(element, searchValue, 0, element.size());
}

template<typename T>
bool binarySearchValueHelper(std::vector<T> & element, const T& searchValue, const size_t startIndex, const size_t endIndex)
{
if (startIndex > endIndex || startIndex == endIndex)
return false;

size_t middle = (startIndex + endIndex) / 2;

if (searchValue == element[middle])
return true;
else if (searchValue < element[middle])
return binarySearchValueHelper(element, searchValue, startIndex, middle - 1);
else if (searchValue > element[middle])
return binarySearchValueHelper(element, searchValue, middle + 1, endIndex);

return false;
}

template<typename T>
void printArray(const std::vector<T> & elements)
{
std::for_each(elements.cbegin(), elements.cend(), [](const T& value) { std::cout << value << " "; });
std::cout << "\n\n";
}
}

#endif
66 changes: 63 additions & 3 deletions Algorithms/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@

enum testCase
{
LINEAR_SEARCH
LINEAR_SEARCH,
BINARY_SEARCH
};

void linearSearchTest();
void binarySearchTest();

int main()
{
testCase test = LINEAR_SEARCH;
testCase test = BINARY_SEARCH;

switch (test)
{
case LINEAR_SEARCH:
linearSearchTest();
break;

case BINARY_SEARCH:
binarySearchTest();
break;

default:
break;
}
Expand Down Expand Up @@ -81,9 +87,63 @@ void linearSearchTest()
position = searchAlgorithms::linearSearchIndex(intList, randomValue);

if (position != -1)
std::cout << randomValue << " is on position " << position << " in list\n";
std::cout << randomValue << " is on position " << position + 1 << " in list\n";
else
std::cout << randomValue << " is not exist in list\n";
}

void binarySearchTest()
{
std::cout << "Linear search test\n\n";

//Initialize a empty vector to store value
std::vector<int> intList;

//initialize random seeds
srand((unsigned int)time(NULL));

//Push value to the list
std::cout << "list contains the followings elements: \n";
int randomValue;
for (int iter = 0; iter < 20; ++iter) {
randomValue = rand() % 250 + 1;
intList.push_back(randomValue);
std::cout << randomValue << " ";
}
std::cout << "\n\n";

//Get one of the value from the list
bool exist = searchAlgorithms::binarySearchValue(intList, intList[10]);
if (exist)
std::cout << intList[10] << " is existed on the list\n";
else
std::cout << intList[10] << " is not existed on the list\n";
std::cout << "\n";

//Check for invalidate value
randomValue = rand() % 500 + 1;
exist = searchAlgorithms::binarySearchValue(intList, randomValue);

if (exist)
std::cout << randomValue << " is existed on the list\n";
else
std::cout << randomValue << " is not existed on the list\n";

std::cout << "\n";
//Get one of the value from the list
int position = searchAlgorithms::binarySearchIndex(intList, intList[8]);
if (position != -1)
std::cout << intList[8] << " is on position " << position << " in list\n";
else
std::cout << intList[8] << " is not exist in list\n";
std::cout << "\n";

//Check for invalidate value
randomValue = rand() % 500 + 1;
position = searchAlgorithms::binarySearchIndex(intList, randomValue);

if (position != -1)
std::cout << randomValue << " is on position " << position + 1 << " in list\n";
else
std::cout << randomValue << " is not exist in list\n";
}

0 comments on commit 9e67c38

Please sign in to comment.