Skip to content

Commit

Permalink
Added bubble sort algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
seancyw committed Jan 26, 2017
1 parent 9e67c38 commit 9bcc1bc
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
44 changes: 44 additions & 0 deletions Algorithms/SortAlgorithms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#ifndef SORTALGORITHMS_H
#define SORTALGORITHMS_H

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

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

for (size_t i = 0; i < elements.size(); ++i)
{
for (size_t j = i + 1; j < elements.size(); ++j) {
if (ascending) {
if (elements[j] < elements[i])
std::swap(elements[i], elements[j]);
}
else {
if (elements[j] > elements[i])
std::swap(elements[i], elements[j]);
}
}
}
}

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 << std::endl;
}

}

#endif
72 changes: 70 additions & 2 deletions Algorithms/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#include <iostream>
#include "SearchAlgorithms.h"
#include "SortAlgorithms.h"
#include <ctime>
#include <random>
#include <vector>

enum testCase
{
LINEAR_SEARCH,
BINARY_SEARCH
BINARY_SEARCH,
BUBBLE_SORT
};

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

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

switch (test)
{
Expand All @@ -27,6 +30,10 @@ int main()
binarySearchTest();
break;

case BUBBLE_SORT:
bubbleSortTest();
break;

default:
break;
}
Expand Down Expand Up @@ -146,4 +153,65 @@ void binarySearchTest()
std::cout << randomValue << " is on position " << position + 1 << " in list\n";
else
std::cout << randomValue << " is not exist in list\n";
}

void bubbleSortTest()
{
std::cout << "Bubble 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::bubbleSort(intList, true);

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

//Sort the list in descending order
sortAlgorithms::bubbleSort(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::bubbleSort(doubleList, true);

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

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

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

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

0 comments on commit 9bcc1bc

Please sign in to comment.