题目来自网络
题目(1):基于数组的归并排序
题目(2):基于链表的归并排序
----------
题目(1):基于数组的归并排序
思路:借助分治的思想,总是先对待排序进行分割,之后再对两个子序列进行合并,生成一个有序序列。
时间复杂度为O(nLogn),空间复杂度为O(n)
代码:
void Merge(int nArr[],int nStart,int nMid,int nEnd)
{
int nCurLeft = nStart;
int nLeftEnd = nMid;
int nCurRight = nMid + 1;
int nRightEnd = nEnd;
int nCur = 0;//易错点
int* nArrMerge = new int[nEnd - nStart + 1];
while(nCurLeft <= nLeftEnd && nCurRight <= nRightEnd)
{
if (nArr[nCurLeft] > nArr[nCurRight])
{
nArrMerge[nCur++] = nArr[nCurRight++];
}
else
{
nArrMerge[nCur++] = nArr[nCurLeft++];
}
}
while (nCurLeft <= nLeftEnd)
{
nArrMerge[nCur++] = nArr[nCurLeft++];
}
while(nCurRight <= nRightEnd)
{
nArrMerge[nCur++] = nArr[nCurRight++];
}
for (int i = 0;i < nCur;i++)//易忘点
{
nArr[i + nStart] = nArrMerge[i];
}
delete[] nArrMerge;
}
void MergeSort(int nArr[],int nStart,int nEnd)
{
if (nStart >= nEnd)
{
return;
}
int nMid = (nStart + nEnd) >> 1;
MergeSort(nArr,nStart,nMid);
MergeSort(nArr,nMid + 1,nEnd);
Merge(nArr,nStart,nMid,nEnd);
}
void MergeSort(int nArr[],int nLen)
{
assert(nArr != NULL && nLen > 0);
MergeSort(nArr,0,nLen - 1);
}
注意:下面代码的
辅助空间是每次归并都申请一次,当然也可以只申请一次。这里代码就不表了。题目(2):基于链表的归并排序
代码
#include <iostream>
using namespace std;
struct ListNode
{
int m_nData;
ListNode* m_pNext;
};
ListNode* Merge(ListNode* pFirst,ListNode* pSec)
{
ListNode* pNewHead = NULL;
ListNode* pCur = NULL;
while(pFirst && pSec)
{
if (pFirst->m_nData > pSec->m_nData)
{
if (pNewHead == NULL)
{
pNewHead = pSec;
pCur = pSec;
}
else
{
pCur->m_pNext = pSec;
pCur = pCur->m_pNext;
}
pSec = pSec->m_pNext;
}
else
{
if (pNewHead == NULL)
{
pNewHead = pFirst;
pCur = pFirst;
}
else
{
pCur->m_pNext = pFirst;
pCur = pCur->m_pNext;
}
pFirst = pFirst->m_pNext;
}
}
if (pFirst == NULL && pSec != NULL)
{
pCur->m_pNext = pSec;
}
else
{
pCur->m_pNext = pFirst;
}
return pNewHead;
}
ListNode* MergeSort(ListNode* pHead)
{
ListNode* pFirstHead = NULL;
ListNode* pSecHead = NULL;
if (pHead == NULL || pHead->m_pNext == NULL)
{
return pHead;
}
if (pHead->m_pNext->m_pNext == NULL)//只有俩元素
{
if (pHead->m_nData > pHead->m_pNext->m_nData)
{
ListNode* pSec = pHead->m_pNext;
pHead->m_pNext = NULL;
pSec->m_pNext = pHead;
return pSec;
}
return pHead;
}
//此时,链表肯定有三个以上的元素
//分成两个子问题
ListNode* pFast = pHead;
ListNode* pSlow = pHead;
while(pFast->m_pNext && pFast->m_pNext->m_pNext)
{
pSlow = pSlow->m_pNext;
pFast = pFast->m_pNext->m_pNext;
}
pSecHead = pSlow->m_pNext;
pSlow->m_pNext = NULL;
pFirstHead = MergeSort(pHead);
pSecHead = MergeSort(pSecHead);
//子问题合并
return Merge(pFirstHead,pSecHead);
}
void Print(ListNode* pHead)
{
while(pHead != NULL)
{
cout<<pHead->m_nData<<" ";
pHead = pHead->m_pNext;
}
cout<<endl;
}
void CreateList(ListNode** pHead,int nLen)//头指针使用指针的指针
{
ListNode* pCur = NULL;
ListNode* pNewNode = NULL;
for (int i = 0;i < nLen;i++)
{
pNewNode = new ListNode;
cin>>pNewNode->m_nData;
pNewNode->m_pNext = NULL;
if (*pHead == NULL)
{
*pHead = pNewNode;
pCur = *pHead;
}
else
{
pCur ->m_pNext = pNewNode;
pCur = pNewNode;
}
}
}
int main()
{
int nLen = 5;
ListNode* pHead = NULL;
CreateList(&pHead,nLen);
pHead = MergeSort(pHead);
Print(pHead);
system("pause");
return 1;
}