关于链表的插入排序

zealot198226 2005-04-01 11:24:01
#include <iostream.h>
#include <stdlib.h>
/* InsertonSort: sort liked list by the insertion sort method.
PRE: The linked list list had been created.Each entry of list contains a key
POST: The entries of the list have been rearranged so that the keys in all the entries are sorted into nondecreasing order.*/
void InsertionSort(List* list)
{
ListNode *fu; /*the first unsorted node to be inserted */
ListNode *Is; /*the last sorted node (tail of sorted sublists) */
ListNode *current,*trailing;
if(list->head){
Is = list->head; /*An empty list is already sorted */
while(Is->next) {
fu=Is->next; /*Remember first unsorted node */
if(LT(fu->entry.key,list->head->entry.key)){
Is->next=fu->next;
fu->next=list->head;
list->head=fu; /*Insert first unsorted at the head of sorted list*/
}else{
trailing=list->head;
for(current=trailing->next; GT(fu->entry.key,current->entry.key);current=current->next)
trailing=current;
/*First unsorted node now belongs between trailing and current */
if(fu== current)
Is=fu;
else{
Is->next=fu->next;
fu->next=current;
trailing->next=fu;
}
}
}
}
}


这是用链表实现插入排序的过程,但是看不清这个循环最后是什么样的变化,有人能分析下这个链表在排序过程中如何变化吗?GT和LT分别是宏代表GREATER THAN 和LESS THAN
...全文
318 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pcboyxhy 2005-04-01
  • 打赏
  • 举报
回复
你拿一个长度为3的链表测试一下
如果觉得很吃力
就想办法练练基本功
zealot198226 2005-04-01
  • 打赏
  • 举报
回复
已经想通了,是因为第二个IF语句的条件没看清。
xxxdg 2005-04-01
  • 打赏
  • 举报
回复
呵呵,有别人问我的同样的问题,
互相参考一下
这个程序也是有问题的,呼呼
xxxdg 2005-04-01
  • 打赏
  • 举报
回复
#include "iostream"
using namespace std;

struct Node
{
int value;
Node * pNext;
};

struct List
{
Node Head;
Node Tail;
};

void Init(List * pList)
{
pList->Head.pNext = &pList->Tail;
pList->Tail.pNext = 0;
pList->Head.value=0;
}
void Insert(Node * pCurrent,Node * pNew)
{
if(pCurrent)
{
pNew->pNext = pCurrent->pNext;
pCurrent->pNext = pNew;
}

}

void Delete(Node * pCurrent,Node * pToDelete)
{
if(pCurrent&&pToDelete&&(pCurrent->pNext==pToDelete))
pCurrent->pNext = pToDelete->pNext;
free(pToDelete);
}

void Destroy(List * pList)
{
while(pList->Head.pNext!=&pList->Tail)
Delete(&pList->Head,pList->Head.pNext);
}

int compare(Node * p1,Node * p2)
{
if(p1->value>=p2->value)
return 1;
else
return 0;
}
void Sort(List * pSource)
{
if(pSource->Head.value>0)
{
Node * pTemp = pSource->Head.pNext;
Init(pSource);
Insert(&pSource->Head,pTemp);
pTemp=pTemp->pNext;
Node * p1,*p2;
while(pTemp!=&pSource->Tail)
{
p2=&pSource->Head;
p1=pSource->Head.pNext;
while(compare(p1,pTemp)&&p1!=&pSource->Tail)
{
p2=p1;
p1=p1->pNext;
}
Insert(p2,pTemp);
pTemp=pTemp->pNext;
}

}
}

int main(int argc, char* argv[])
{

List l;
Init(&l);
Node * p;
for(int i=0;i<10;i++)
{
p=(Node *)malloc(sizeof(Node));
p->value=i;
Insert(&l.Head,p);
cout<<p->value<<" ";
}
cout<<endl;
Sort(&l);
for(p=l.Head.pNext;p!=&l.Tail;p=p->pNext)
cout<<p->value<<" ";
cout<<endl;
Destroy(&l);
return 0;
}
zealot198226 2005-04-01
  • 打赏
  • 举报
回复
UP
zealot198226 2005-04-01
  • 打赏
  • 举报
回复
顶下。
zealot198226 2005-04-01
  • 打赏
  • 举报
回复
这个链表算法我觉得是有问题的,比如假设头节点是最大为8,后面按照3,4,5,6,7来排列的话最后得到是7,6,5,4,3,8,只是把8排到最后了而已。这是刚才从新又走了一遍感觉的结果,不知道有没有问题。

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧