Insertion Sort List


Sort a linked list using insertion sort.


通过前面几个list的题目,画节点,显示节点,这一次,自己完全独立,accepted的插入排序法排序题目。

做这个题目,首先理解一下,插入排序法::


插入排序法的思想,已经知道一个序列是有序的,将新来的一个数,插入有序的序列中,使其有序。

比较简单的代码,可以参考插入排序的,数组排序法。

我找一个类比的代码::

数组插入排序,可以理解为a[0]是有序的,依次把后面的数字插入进来,最后形成有序的a

数组 的插入排序起代码如下,可以测试,理解思想。  然后再改写链表的插入排序法,相对来说就比较容易了。

void insertionArray(int a[],int length)
{
	 int i;
	 int tmp;
	for (i=1;i<length;i++)
	{
	
		//将a[i]插入前面有序的数组中比较
		for (int j=i-1;j>=0;j--)
		{   
		   cout<<"插入排序前: "<<endl;
		    PrintArr(a, length-1);  
			
			if (a[j]>a[j+1])
			{
                int tmp=a[j];
				a[j]=a[j+1];
				a[(j+1)]=tmp;
			}
		}
	}
}

下面是

 struct ListNode* insertionSortListThree(struct ListNode* head);
struct ListNode* insertionSortList(struct ListNode* head) 
{
	if (head==NULL||head->next==NULL)
	{
		return head;
	}
	struct ListNode* pCurrentNoSequence=head;
	struct ListNode* pCurrentSequenced=insertionSortListThree(head);
    struct ListNode* pCurrentSequenceHead=pCurrentSequenced;
	struct ListNode* pCurrentSequenceHeadpCurrent=pCurrentSequenced;

	if (pCurrentNoSequence->val<=pCurrentSequenced->val)
		return pCurrentNoSequence;

	//把排序号的放在左边了,有一种查到到位置,然后插入...
	struct ListNode* pCurrentSequenceNext=pCurrentSequenced->next;  
	
		while(1)
		{
			if(pCurrentSequenceNext==NULL||(pCurrentNoSequence->val>pCurrentSequenced->val&&pCurrentNoSequence->val<=pCurrentSequenceNext->val))
				{ 
					
					pCurrentNoSequence->next=pCurrentSequenced->next;  //让后面的到前面来.
					pCurrentSequenced->next=pCurrentNoSequence;  //把没排序的放在排序的后面
					//	pCurrentfinished1->next=pCurrentInsert;  //把这个插入的点放在后面.
					//return pCurrentSequenced;
					pCurrentSequenced=pCurrentNoSequence->next;
	    			break;
				}
			else
			{
                pCurrentSequenced=pCurrentSequenced->next;
               pCurrentSequenceNext=pCurrentSequenceNext->next;
			}
		}
	//后面pCurrentSequenceHead段开需要来接起...
	//放在组后面,和放在中间也不一样?
	return pCurrentSequenceHead;
}

struct ListNode* insertionSortListThree(struct ListNode* head) 
{
	if (head->next==NULL)
	{
		return head;
	}
	//使用递归,否则就往下执行
	//假设第一个节点是有序的
	struct ListNode* pCurrentInsertFront=head;
	struct ListNode* pCurrentInsert=head->next;  //已经完成的排序序列

	struct ListNode* pCurrentfinished1=insertionSortListThree(pCurrentInsert);
	struct ListNode* pCurrentFinishHead=pCurrentfinished1;
   //finished位空?

	if (pCurrentInsert->val<=pCurrentfinished1->val)
		return pCurrentInsert;
	//当他在已经排好序列的中间的事情,还有大于只有一次.
	while (pCurrentfinished1!=NULL)
	{
		if (pCurrentInsert->val>pCurrentfinished1->val)
		{
			//把这个节点段掉,
			pCurrentInsertFront->next=pCurrentInsert->next;  //让后面的到前面来.
			pCurrentInsert->next=pCurrentfinished1->next;
			pCurrentfinished1->next=pCurrentInsert;  //把这个插入的点放在后面.
			//插入节点插入到前面去...	 
			pCurrentInsertFront=pCurrentfinished1;
			pCurrentfinished1=pCurrentInsert->next;
			
		}
		else
			break;
		
	}
	return pCurrentFinishHead;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值