Skip to content

Commit

Permalink
Complete merge sort implement (have bugs in merge operation)
Browse files Browse the repository at this point in the history
  • Loading branch information
seancyw committed Jan 31, 2017
1 parent 52dfe22 commit 0d484c7
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 23 deletions.
78 changes: 57 additions & 21 deletions Algorithms/SortAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,48 +164,84 @@ namespace sortAlgorithms
return;
}

sortHelper(ascending, 0, elements.size());
sortHelper(0, static_cast<int>(elements.size()) - 1, ascending);
}



void merge(bool ascending, int lowIndex, int middleIndex, int highIndex)
void merge(int lowIndex, int middleIndex, int highIndex, bool ascending)
{
//Merge elements[lowIndex...middleIndex]
//with elements[middleIndex+1...highIndex]
int i = lowIndex;
int j = middleIndex + 1;

std::vector<T> mergeArray(elements);

//Merge back to elements[low..highIndex]
for (int k = lowIndex; k <= highIndex; ++k) {
if (i > middleIndex)
elements[k] = mergeArray[j++];
else if(j > highIndex)
elements[k] = mergeArray[i++];
else if (lesser(mergeArray[j], mergeArray[i]) && ascending)
elements[k] = mergeArray[j++];
else
elements[k] = mergeArray[i++];
//Index into left subvector
int leftIndex = lowIndex;

//Index into right subvector
int rightIndex = middleIndex + 1;

//Index into temporary working vector
int combinedIndex = lowIndex;
std::vector<T> mergeArray(elements.size());

//Merge vectors until reaching end of either
while (leftIndex <= middleIndex && rightIndex <= highIndex) {

//Place smaller of two current elements into result and
//move to next space in vector
if (ascending) {
if (elements[leftIndex] <= elements[rightIndex])
mergeArray[combinedIndex++] = elements[leftIndex++];
else
mergeArray[combinedIndex++] = elements[rightIndex++];
}
else {
if (elements[rightIndex] >= elements[leftIndex])
mergeArray[combinedIndex++] = elements[rightIndex++];
else
mergeArray[combinedIndex++] = elements[leftIndex++];
}
}

//if at end of left vector
if (leftIndex == rightIndex) {
//copy in rest of right vector
while (rightIndex <= highIndex)
mergeArray[combinedIndex++] = elements[rightIndex++];
}
else { //at end of right vector
//copy in rest of left vector
while (leftIndex <= middleIndex)
mergeArray[combinedIndex++] = elements[leftIndex++];
}

//copy values back to original vector
for (int i = lowIndex; i <= highIndex; ++i)
elements[i] = mergeArray[i];
}

void printArray() const
{
std::for_each(elements.cbegin(), elements.cend(), [](const T & value) { std::cout << value << " "; });
std::cout << std::endl;
}

private:
std::vector<T> elements;

void sortHelper(int lowIndex, int highIndex, bool ascending)
{
if (highIndex <= lowIndex)
//test base case
//if size of vector equals to 1
if ((highIndex - lowIndex) < 1)
return;

//Calculate the middle of vector
int middle = (lowIndex + highIndex) / 2;

//Sort left half
sortHelper(lowIndex, middle, ascending);

//Sort right half
sortHelper(middle + 1, highIndex, ascending);

merge(lowIndex, middle, highIndex, ascending);
}
};

Expand Down
73 changes: 71 additions & 2 deletions Algorithms/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ enum testCase
BUBBLE_SORT,
SELECTION_SORT,
INSERTION_SORT,
SHELL_SORT
SHELL_SORT,
MERGE_SORT
};

void linearSearchTest();
Expand All @@ -21,10 +22,11 @@ void bubbleSortTest();
void selectionSortTest();
void insertionSortTest();
void shellSortTest();
void mergeSortTest();

int main()
{
testCase test = SHELL_SORT;
testCase test = MERGE_SORT;

switch (test)
{
Expand Down Expand Up @@ -52,6 +54,10 @@ int main()
shellSortTest();
break;

case MERGE_SORT:
mergeSortTest();
break;

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

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

void mergeSortTest()
{
std::cout << "Merge 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::MergeSort<int> mergeIntSort(intList);
mergeIntSort.sort(true);

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

//Sort the list in descending order
mergeIntSort.sort(false);

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

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::MergeSort<double> mergeDoubleSort(doubleList);
mergeDoubleSort.sort(true);

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

//Sort the list in descending order
mergeDoubleSort.sort(false);

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

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

0 comments on commit 0d484c7

Please sign in to comment.