There are a few ways to do a merge sort, but I specifically need it to work like a natural merge sort. What happens in this algorithm is that smaller lists of numbers are generated in the file like so:
原件:35 27 24 28 31 37 1 4 7 6 8 9
清单小部分:[35]、[27]、[24、28]、[31、37]、[1、4、7]、[6、8、9]
As you notice, everytime the unsorted list comes across a number that s smaller than it s present value, a new unit is created. Once the list ends, this list breaks into two other lists like so in an alternative fashion:
第一清单:[35]、[24、28]、[1、4、7]
第二份清单:[27]、[31、37]、[6、8、9]
最后一个步骤是再次将这两个清单合并起来,而第一个清单项目中的单位与第二个清单项目之间的数值比较。 例如,清单一中的第一个单位与清单二中的第一个单位比较,并按顺序排列。 任何左边单位将在尾添加(未显示)。
合并两个清单:[27、35]、[24、28、31、37]、[1、4、6、7、8、9]
This process will repeat until the list is sorted in one unit.
除了合并两个清单外,我在本算法中确定了一切工作要做,而且很难解答或找到问题。 它经过了近一半的时间,才陷入分裂的错误。 不管怎么说,我可以把STL的合并用于该方案,而且它都列在一个链接名单上。
www.un.org/Depts/DGACM/index_spanish.htm 注:建筑商和其他必要功能已经到位。 我没有履行其他职能,以体现具体职能。
class mergeList
{
public:
//Setters
void setNumber(int item);
void setStart(bool newStatus);
void setEnd(bool newStatus);
void setPrev(mergeList* node);
void setNext(mergeList* node);
//Getters
int getNumber();
bool getStart();
bool getEnd();
mergeList* getPrev();
mergeList* getNext();
private:
int number;
bool startSection;
bool endSection;
mergeList* prev;
mergeList* next;
};
class mergeListObject
{
public:
mergeList* firstNode;
mergeList* lastNode;
void mergeLists(mergeListObject &firstList,
mergeListObject &secondList);
//Other functions in program are in here.
};
void mergeListObject::mergeLists(mergeListObject &firstList,
mergeListObject &secondList)
{
mergeList* combTraverse = firstNode;
mergeList* firstTraverse = firstList.firstNode;
mergeList* secondTraverse = secondList.firstNode;
bool listDone = false;
//This will clear the combination list for insertion
while (combTraverse != NULL)
{
combTraverse->setNumber(-1);
combTraverse->setStart(false);
combTraverse->setEnd(false);
combTraverse = combTraverse->getNext();
}
combTraverse = firstNode;
combTraverse->setStart(true);
while (listDone == false)
{
//This will go until the first list is traversed.
do
{
bool exception = false;
int j = firstTraverse->getNumber();
int k;
//If the second list is still active, this will
//grab its current value for comparison.
if (secondTraverse != NULL)
k = secondTraverse->getNumber();
//Second list is done, will automatically grab
//first list s value and traverse through to the end.
if (secondTraverse == NULL)
combTraverse->setNumber(firstTraverse->getNumber());
else
{
//Both values from both lists are compared.
if (j <= k)
combTraverse->setNumber(firstTraverse->getNumber());
else
{
exception = true;
combTraverse->setNumber(secondTraverse->getNumber());
}
}
//If the first value unit was used, move the first list iterator up.
//Otherwise, the second list s iterator moves up.
if (exception == false)
firstTraverse = firstTraverse->getNext();
else
secondTraverse = secondTraverse->getNext();
exception = false;
}
while (firstTraverse->getEnd() == false);
//If the second list isn t done, this will finish it.
do
{
combTraverse->setNumber(secondTraverse->getNumber());
secondTraverse = secondTraverse->getNext();
combTraverse = combTraverse->getNext();
}
while (secondTraverse->getEnd() == false);
combTraverse->setEnd(true);
//Marks the end of the section and sets the next one,
//considering it isn t the end of the list.
if (combTraverse->getNext() != NULL)
combTraverse->getNext()->setStart(true);
//Both of these should end up at the start of a unit in their own lists.
firstTraverse = firstTraverse->getNext();
secondTraverse = secondTraverse->getNext();
//Are both lists done?
if (firstTraverse == NULL &&
secondTraverse == NULL)
listDone = true;
}
return;
}
int main()
{
mergeListObject one;
mergeListObject two;
mergeListObject combined;
//The two lists are already separated. All
//other functions have already been called.
combined.mergeLists(one, two);
return 0;
}