• 自顶向下归并排序的应用——链表的随机化


         在对一个链表进行随机化时,可以借鉴自顶向下归并排序的思路,先将链表划分为左右两个子链表,分别进行随机化,然后再归并两个子链表,并在归并过程中进行随机化,代码如下:

    // *pHead指向链表的首节点(不是头结点)
    void RandomizeLinkedList(Link * pHead)
    {
    	int length = CountNodes(* pHead);
    	Randomize(pHead,length);
    
    	Link tail = * pHead;
    	
        for(int i = 1;i < length;i++)
    	{
    		tail = tail->next;
    	}
    	
        tail->next = NULL;
    }
    
    // 随机化首节点由*pHead指向,长度为length的链表,随机化完成后*pHead指向链表新的首节点。该函数类似于自顶向下的归并排序
    Randomize(Link * pHead,int length)
    {
    	if(length == 1)
    	{
    		return;
    	}
    
    	int lengthOfLeftSublist = length / 2;
        int lengthOfRightSublist = length - lengthOfLeftSublist;
    	
    	Link headOfRightSublist =  * pHead;
    	for(int i = 0;i < countOfLeftSublist;i++)
    	{
    		headOfRightSublist = headOfRightSublist->next;
    	}
    
    	Randomize(pHead,lengthOfLeftSublist);
    	Randomize(&headOfRightSublist,lengthOfRightSublist);
    
    	Merge(pHead,length,lengthOfLeftSublist,headOfRightSublist);
    }
    
    // 链表的首结点由*pHead指向,长度为length,对其已经完成随机化的左右子链表进行归并,归并完成后*pHead指向链表新的首节点
    void Merge(Link * pHead,int length,int lengthOfLeftSublist,Link headOfRightSublist)
    {
    	Link headOfLeftSublist = * pHead;
    
    	if(GetProbability((double)lengthOfLeftSublist/length)))
    	{
    		headOfLeftSublist = headOfLeftSublist->next;
    		lengthOfLeftSublist--;
    	}
    	else
    	{
    		* pHead = headOfRightSublist;
    		headOfRightSublist = headOfRightSublist->next;
    	}
    
    	length--;
    
    	Link tail = * pHead;
    	
    	while(lengthOfLeftSublist > 0)
    	{
    		if(GetProbability((double)lengthOfLeftSublist/length)))
    		{
    			tail->next = headOfLeftSublist;
    			headOfLeftSublist = headOfLeftSublist->next;
    			lengthOfLeftSublist--;
    		}
    		else
    		{
    			tail->next = headOfRightSublist;
    			headOfRightSublist = headOfRightSublist->next;
    		}
    
    		length--;
    		tail = tail->next;
    	}
    
    	if(length > 0)
    	{
    	    tail->next = headOfRightSublist;
    	}
    }
    
    bool GetProbability(double probability)
    {
    	return  rand() / RAND_MAX < probability;
    }
    

      

  • 相关阅读:
    语文作业
    在Windows下为PHP5.5安装redis扩展
    PHP安全处理之Mcrypt使用总结
    CSS3 动画
    微信开发--获取用户openID
    微信公众号开发--获取用户信息
    php 中gd库的安装
    随机输入几个数,输出重复的数字和重复的次数
    永久解决"Error:java: 错误: 不支持发行版本 12"的问题
    报"java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing"的原因和如何处理
  • 原文地址:https://www.cnblogs.com/laifeiyao/p/3475056.html
Copyright © 2020-2023  润新知