Skip to content

Commit

Permalink
- Added Selection Sort implementation
Browse files Browse the repository at this point in the history
- Fixed warnings
- Remove unused files in directory
  • Loading branch information
seancyw committed Jan 27, 2017
1 parent 9bcc1bc commit d2db965
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 16 deletions.
4 changes: 0 additions & 4 deletions Algorithms/Algorithms.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,10 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="SearchAlgorithms.h" />
<ClInclude Include="SearchValidation.h" />
<ClInclude Include="SortAlgorithms.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="SearchAlgorithms.cpp" />
<ClCompile Include="SearchValidation.cpp" />
<ClCompile Include="SortAlgorithms.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
6 changes: 0 additions & 6 deletions Algorithms/Algorithms.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
<ClInclude Include="SortAlgorithms.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SearchValidation.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="SearchAlgorithms.cpp">
Expand All @@ -35,8 +32,5 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SearchValidation.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Algorithms/SearchAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace searchAlgorithms {
if (list.size() == 0)
return -1;

for (size_t index = 0; index < list.size(); ++index) {
for (int index = 0; index < static_cast<int>(list.size()); ++index) {
if (list[index] == searchValue)
return index;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ namespace searchAlgorithms {
if (startIndex > endIndex || startIndex == endIndex)
return -1;

size_t middle = (startIndex + endIndex) / 2;
int middle = static_cast<int>(startIndex + endIndex) / 2;

if (searchValue == element[middle])
return middle;
Expand Down
56 changes: 54 additions & 2 deletions Algorithms/SortAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,73 @@ namespace sortAlgorithms
return;
}

//Use to check if the list is sorted
//Return directly if the list is sorted
bool isSorted = true;
for (size_t i = 0; i < elements.size(); ++i)
{
isSorted = true;

for (size_t j = i + 1; j < elements.size(); ++j) {
if (ascending) {
if (elements[j] < elements[i])
//Swap elements if the elements if current elements is
//lesser than the previous
if (elements[j] < elements[i]) {
std::swap(elements[i], elements[j]);
isSorted = false;
}
}
else {
if (elements[j] > elements[i])
//Swap elements if the elements if current elements is
//greater than the previous
if (elements[j] > elements[i]) {
std::swap(elements[i], elements[j]);
isSorted = false;
}
}
}

//Break the loops if the list is sorted
if (isSorted)
break;
}
}

template<typename T>
void selectionSort(std::vector<T> & elements, bool ascending)
{
if (elements.size() == 0) {
throw std::exception("Error! Cannot process empty array.\n");
return;
}

//Determine which elements to swap with the current index
//element for minimum or maximum
size_t index = 0;

for (size_t i = 0; i < elements.size(); ++i) {

index = i;

for (size_t j = i + 1; j < elements.size(); ++j) {
if (ascending) {
if (elements[j] < elements[index])
index = j;
}
else {
if (elements[j] > elements[index])
index = j;
}
}

//Swap the elements with the current index for the minimum
//or maximum
if(i != index)
std::swap(elements[i], elements[index]);
}
}


template<typename T>
void printArray(const std::vector<T> & elements)
{
Expand Down
71 changes: 69 additions & 2 deletions Algorithms/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ enum testCase
{
LINEAR_SEARCH,
BINARY_SEARCH,
BUBBLE_SORT
BUBBLE_SORT,
SELECTION_SORT
};

void linearSearchTest();
void binarySearchTest();
void bubbleSortTest();
void selectionSortTest();

int main()
{
testCase test = BUBBLE_SORT;
testCase test = SELECTION_SORT;

switch (test)
{
Expand All @@ -34,6 +36,10 @@ int main()
bubbleSortTest();
break;

case SELECTION_SORT:
selectionSortTest();
break;

default:
break;
}
Expand Down Expand Up @@ -213,5 +219,66 @@ void bubbleSortTest()
std::cout << "\nAfter sorting in descending order, the lists contains: ";
sortAlgorithms::printArray(doubleList);

std::cout << "\n";
}

void selectionSortTest()
{
std::cout << "Selection sort test\n\n";

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

//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";

//Sort the list in ascending order
sortAlgorithms::selectionSort(intList, true);

std::cout << "\nAfter sorting in ascending order, the lists contains: ";
sortAlgorithms::printArray(intList);

//Sort the list in descending order
sortAlgorithms::selectionSort(intList, false);

std::cout << "\nAfter sorting in descending order, the lists contains: ";
sortAlgorithms::printArray(intList);

std::cout << "\n";

//Push value to the list
std::cout << "list contains the followings elements: \n";

double doubleRandomValue;
for (int iter = 0; iter < 20; ++iter) {
doubleRandomValue = (rand() % 500 + 1) + iter / 10.;
doubleList.push_back(doubleRandomValue);
std::cout << doubleRandomValue << " ";
}
std::cout << "\n";

//Sort the list in ascending order
sortAlgorithms::selectionSort(doubleList, true);

std::cout << "\nAfter sorting in ascending order, the lists contains: ";
sortAlgorithms::printArray(doubleList);

//Sort the list in descending order
sortAlgorithms::selectionSort(doubleList, false);

std::cout << "\nAfter sorting in descending order, the lists contains: ";
sortAlgorithms::printArray(doubleList);

std::cout << "\n";
}

0 comments on commit d2db965

Please sign in to comment.