单链中的归并排序和快速排序

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);
	}
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言,实现快速排序(QuickSort)算法通常用于数组或动态数组(如数组或动态分配的内存块),因为它们支持随机访问。然而,对于单链表这种顺序存储结构,由于不支持直接索引,快速排序的传统方法并不适用,因为快速排序依赖于元素的原地交换。对于链表,更合适的是使用迭代或递归的方式来重新排列节点,但这已经不再是标准的快速排序,而是类似于“分治”的思想,但操作方式有所不同。 一种常见的链表排序算法是归并排序,因为它适合链表的结构,不需要频繁的元素交换。如果要用类似快速排序的思想对链表进行排序,通常会采用“分而治之”的策略,先分割链表,然后递归地处理子链表,最后合并结果。但这种实现会涉及到递归和链表的操作,比较复杂。 如果你想了解如何在链表上实现类似分治的排序,可以考虑以下步骤: 1. **选择基准**:在链表选择一个节点作为基准,通常是头节点或者随机节点。 2. **分割链表**:将链表分为两部分,一部分包含所有小于基准的节点,另一部分包含所有大于等于基准的节点。 3. **递归排序**:对这两部分链表分别进行递归排序。 4. **合并链表**:将两个已排序的链表合并成一个有序链表。 这里需要注意的是,没有原地交换操作,链表的合并可能会涉及到链表节点的插入和删除操作。 如果你对具体的代码实现感兴趣,我可以提供一个简化版的伪代码,但完整的C语言代码实现会涉及较多细节,包括链表节点的结构定义和递归调用。如果你想要详细的代码示例,请告诉我,我会尽力为你提供帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值