百分求链表的排序算法,要求速度最快!谢谢大侠

Caps77 2005-04-27 03:19:50
百分求链表的排序算法,要求速度最快!谢谢大侠
...全文
391 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
homeforrose 2005-04-29
  • 打赏
  • 举报
回复
记得去年我有一个朋友去华为面试,考官就让他用快速排序对链表进行排序.结果没有入用!
pcboyxhy 2005-04-29
  • 打赏
  • 举报
回复
链表转化为数组 O(n)
然后快速排序
或者堆排序
O(n*logn)
然后再转化为链表 O(n)

时间复杂度为O(n*logn)
darkstar21cn 2005-04-29
  • 打赏
  • 举报
回复
不能随机存取,只有用冒泡排序了。
MagicCarmack 2005-04-29
  • 打赏
  • 举报
回复
用stl
WingForce 2005-04-28
  • 打赏
  • 举报
回复
要是我来做就用stl
不去伤脑筋了
TemplatesGuy 2005-04-28
  • 打赏
  • 举报
回复
MK
fire314159 2005-04-28
  • 打赏
  • 举报
回复
快速排序。如果是链表的话,用堆排序效果很好,时间复杂度为nlogn
improgrammer 2005-04-28
  • 打赏
  • 举报
回复
要是我来做,要么把链表换成数组,要么就用插入排序法--链表能有多长啊?不去伤脑筋。
QuickKeyBoard 2005-04-28
  • 打赏
  • 举报
回复
我给出一个快速排序的方案,不过这个方案要把单向链表改为双向的才可以:
代码如下:
void quick(node *s,node *e)
{
node *i,*j;
i=s;
j=e;
int t;
t=i->data;
while(i!=j)
{
while(j->data>=t && i!=j)
 j=j->prev;
i->data=j->data;
while(i->data<=t && i!=j)
 i=i->next;
j->data=i->data;
}
i->data=t;
if (s!=i && s->next!=i && i->prev!=NULL)
quick(s,i->prev);
if (i!=e && i->next!=e && i->next!=NULL)
quick(i->next,e);
}
hikuers 2005-04-27
  • 打赏
  • 举报
回复
/* 堆排序的算法源程序*/


#define MAXNUM 100
#include<stdio.h>
typedef int KeyType;

typedef int DataType;

typedef struct

{ KeyType key; /* 排序码字段 */

/*DataType info; 记录的其它字段 */

}RecordNode;

typedef struct

{ RecordNode record[MAXNUM];

int n; /* n为文件中的记录个数,n<MAXNUM */

}SortObject;


#define leftChild(i) 2*(i)+1

void sift(SortObject * pvector, int i, int n)

{int child; RecordNode temp =pvector->record[i];

child=leftChild(i); /* Rchild是R0的左子女 */

while(child<n)

{if((child<n-1)&&(pvector->record[child].key<pvector->record[child+1].key))

child++; /* child 指向Ri的左、右子女中排序码较大的结点 */

if(temp.key<pvector->record[child].key)

{ pvector->record[i]=pvector->record[child];

/* 将Rchild换到父结点位置,进入下一层继续调整*/

i=child; child=leftChild(i);

}

else break; /* 调整结束 */

}

pvector->record[i]=temp; /* 将记录Ri放入正确位置 */

}

void heapSort(SortObject * pvector) /* 对记录R0到Rn-1进行堆排序 */

{int i, n;RecordNode temp; n=pvector->n;

for(i=n/2-1; i>=0; i--)

sift(pvector,i,n); /* 建立初始堆 */

for(i=n-1; i>0; i--) /* 进行n-1趟堆排序 */

{temp=pvector->record[0]; /* 当前堆顶记录和最后一个记录互换 */

pvector->record[0]=pvector->record[i];

pvector->record[i]=temp;

sift(pvector,0,i); /* 从R0到Ri-1重建堆 */

}

}

SortObject vector={49,38,65,97,76,13,27,49};

int main(){
int i;
vector.n=8;
heapSort(&vector);
for(i=0;i<8;i++)
printf("%d ",vector.record[i]);
return 0;
}
useresu 2005-04-27
  • 打赏
  • 举报
回复
忘了把节点的结构写出来
#define SIZE 200
typedef struct{
ElemType elem; /*元素类型*/
int next; /*指针项*/
}NodeType; /*表结点类型*/
typedef struct{
NodeType r[SIZE]; /*静态链表*/
int length; /*表长度*/
}L_TBL; /*静态链表类型*/
useresu 2005-04-27
  • 打赏
  • 举报
回复
int Partition(S_TBL *tbl,int low,int high) /*一趟快排序*/
{ /*交换顺序表tbl中子表tbl->[low…high]的记录,使支点记录到位,并反回其所在位置*/
/*此时,在它之前(后)的记录均不大(小)于它*/
tbl->r[0]=tbl->r[low]; /*以子表的第一个记录作为支点记录*/
pivotkey=tbl->r[low].key; /*取支点记录关键码*/
while(low<higu) /*从表的两端交替地向中间扫描*/
{ while(low<high&&tbl->r[high].key>=pivotkey) high--;
tbl->r[low]=tbl->r[high]; /*将比支点记录小的交换到低端*/
while(low<high&&tbl-g>r[high].key<=pivotkey) low++;
tbl->r[low]=tbl->r[high]; /*将比支点记录大的交换到低端*/
}
tbl->r[low]=tbl->r[0]; /*支点记录到位*/
return low; /*反回支点记录所在位置*/
}
void QSort(S_TBL *tbl,int low,int high) /*递归形式的快排序*/
{ /*对顺序表tbl中的子序列tbl->[low…high]作快排序*/
if(low<high)
{ pivotloc=partition(tbl,low,high); /*将表一分为二*/
QSort(tbl,low,pivotloc-1); /*对低子表递归排序*/
QSort(tbl,pivotloc+1,high); /*对高子表递归排序*/
}
}

useresu 2005-04-27
  • 打赏
  • 举报
回复
没有最快的排序方法啊,
视具体情况而定的,
跟你原始序列的顺序,和数据量大小都有关系的.
一般情况下认为快速排序是嘴快的
但是数据量大的话,堆排序就有优势了
useresu 2005-04-27
  • 打赏
  • 举报
回复
啊,没有最快的排序吧,
不同的情况要选择不同的排序方法的,
快速排序在一般情况下认为是最快的,
但是数据量大的话,
堆排序就又有优势了.
hash表也是很好的排序方法.
zengwujun 2005-04-27
  • 打赏
  • 举报
回复
#include <malloc.h>
#include <stdio.h>
struct node
{
int data;
struct node *next;
};

//冒泡排序
void sort(node* head)
{
node *pre,*p1,*p2;
node *tail=0;

int nLen=0;//链表长度
p1=head;
while(p1)
{
nLen++;
p1=p1->next;
}

if(nLen==0 || nLen==1)return;
p1=head;p2=p1->next;
//对nLen长的链表,执行nLen-1次冒泡
for(int i=1;i<nLen;++i)
{
while(p2!=tail)
{
//交换
if(p1->data > p2->data)
{
if(p1==head){
p1->next=p2->next;
p2->next=p1;
pre=p2;
p1=pre->next;p2=p1->next;
}
else
{
pre->next=p2;
p1->next=p2->next;
p2->next=p1;
p1=pre->next;p2=p1->next;
}
}
pre=p1;p1=p1->next;p2=p2->next;
}
//一趟排序完毕,tail指向已排好序部分的第一个
tail=p1;p1=head;p2=p1->next;
}
}

void main()
{
int a[8]={2,4,7,8,6,23,80,11};

node *head=0,*p,*pre;
for(int i=0;i<8;++i)
{
p=(node*)malloc(sizeof(node));
p->data=a[i];
p->next=0;
if(head==0)
head=pre=p;
else
{
pre->next=p;
pre=p;
}
}

printf("Before sort:");
p=head;
while(p)
{
printf("%4d",p->data);
p=p->next;
}
printf("\n");

sort(head);
printf("Before sort:");
p=head;
while(p)
{
printf("%4d",p->data);
p=p->next;
}
printf("\n");
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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