1.先搞一个单链表的快速排序,不能靠交换value来进行排序。这是百度的一道电面题,当时答得不完整。说一下自己的思路:以表中的第一个结点为比较标准,进行分类,分成两个链表,第一个表中的value都比标准的value小,第二个表中的value都比标准的value要大,注意在分的过程中,要将两个链表切断。然后用递归的方法对两个子表进行排序。。。最后把整个表连接起来。
下面给出代码,试了几个数据都可以。但也可能会有bug,求指教。
#include <iostream>
#include <assert.h>
using namespace std;
/ ***Problem : fast sort on Single linked list
Author : Yeepom
Time : 3-29-2013
Description : how to run fast sort on a single linked list, we cannot swap the value in the struct either.
*** /
struct Node
{
int value;
Node* next;
};
void partition(Node* &head, Node* &pHead, Node* &qHead)
{
assert(head!=NULL);
/ *
if (head->next == NULL)
{
return NULL;
}* /
Node* pPre = head;
int temp = head->value;
Node* pCur = head->next;
Node* q = qHead;
while(pCur != NULL)
{
while (pCur != NULL && pCur->value <= temp)
{
if (pHead == NULL)
{
pHead = pCur;
pPre = pPre->next;
pCur = pCur->next;
}
else
{
pCur = pCur->next;
pPre = pPre->next;
}
}
if (pCur == NULL)
{
break;
}
if (qHead == NULL)
{
qHead = pCur;
q = qHead;
}
else
{
q->next = pCur;
q = pCur;
}
pPre->next = pCur->next;
pCur = pPre->next;
}
if (q!=NULL) //error if q is NULL.have found this error while testing the array[5, 4, 3, 2, 1].
{
q->next = NULL;
}
}
void fastSort(Node* &head)
{
if (head == NULL || head->next ==NULL)
{
return;
}
Node* pivot = head;
Node* p = NULL;
Node* q = NULL;
partition(head, p, q);
fastSort(p);
fastSort(q);
if (p == NULL)
{
pivot->next = q;
head = pivot;
return;
}
else if (q == NULL)
{
Node* pi = p;
while (p->next != NULL)
{
p = p->next;
}
p->next = pivot;
pivot->next = NULL;
head = pi;
return;
}
else
{
Node* pi = p;
while (p->next != NULL)
{
p = p->next;
}
p->next = pivot;
pivot->next = q;
head = pi;
return;
}
}
void main()
{
Node* head = (Node*)malloc(sizeof(Node));
Node* p = head;
int a[10] = {9, 8, 7, 4, 5, 2, 1, 3, 6, 0};
for (int i = 0; i < 10; i++)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->value = a[i];
p->next = newNode;
p = p->next;
}
p->next = NULL;
fastSort(head->next);
p = head->next;
for (int i = 0; i < 10; i++,p = p->next)
{
printf("%d ", p->value);
}
}
2.说一下归并吧。归并排序在链表中的应用如鱼得水,非但保持了它O(nlogn)的时间复杂度,而且它在数组排序中广受诟病的空间复杂度在链表排序中也从O(n)降到了O(1)。
好了,上代码了。
#include <iostream>
#include <assert.h>
using namespace std;
/***Problem : mergesort on Single linked list
Author : Yeepom
Time : 3-29-2013
Description : how to run mergesort on a single linked list, we cannot swap the value in the struct either.
***/
struct Node
{
int value;
Node* next;
};
Node*& merge(Node* &p, Node* &q)
{
Node* head = NULL;
Node* cur = head;
while (p != NULL && q != NULL)
{
if (p->value < q->value)
{
if (head == NULL)
{
head = p;
cur = head;
p = p->next;
}
else
{
if (cur->next == p)
{
cur = cur->next;
p = p->next;
}
else
{
cur->next = p;
p = p->next;
}
}
}
else
{
if (head == NULL)
{
head = q;
cur = head;
q = q->next;
}
else
{
if (cur->next == q) //error occured, cause of the time of cur->next == q
{
cur = cur->next;
q = q->next;
}
else
{
cur->next = q;
q = q->next;
}
}
}
}
if (p != NULL)
{
cur->next = p;
}
else if (q != NULL)
{
cur->next = q;
}
else
{
cur->next = NULL;
}
return head;
}
void mergeSort(Node* &head)
{
if (head->next == NULL)
{
return;
}
Node* pFast = head;
Node* pSlow = head;
while (pFast->next !=NULL && pFast->next->next != NULL) //error..what we added is pFast->next != NULL..
{
pFast = pFast->next->next;
pSlow = pSlow->next;
}
Node* p = head;
Node* q = pSlow->next;
pSlow->next = NULL;
mergeSort(p);
mergeSort(q);
head = merge(p, q);
}
void main()
{
Node* head = (Node*)malloc(sizeof(Node));
Node* p = head;
int a[5] = {9, 8, 7, 5, 4};
for (int i = 0; i < 5; i++)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->value = a[i];
p->next = newNode;
p = p->next;
}
p->next = NULL;
mergeSort(head->next);
p = head->next;
for (int i = 0; i < 5; i++,p = p->next)
{
printf("%d ", p->value);
}
}