没法了,大家救命啊!!(115分奉上)

fanz2000 2001-12-22 03:16:19
快到交课程设计的时候了
怎么用c语言对一个单链表的关键字排序啊,
最好再另辟一个空间
有谁写过啊,
麻烦哪位给我一个自己写过的这种功能的函数或程序啊
115分奉上
...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
z_yheart 2001-12-22
  • 打赏
  • 举报
回复
快速排序实现
作者:ilovec 2001.10.6

#include <stdio.h>
#include <math.h>
#include <process.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>

#define TRUE 1
#define FALSE 0
#define OK 2
#define ERROR -1
#define INFEASIBLE -2
#define OVERFLOW -3
#define MAXSIZE 20

typedef int status;
typedef int keytype;
typedef struct {
keytype key;
}elemtype;

#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)< (b))
#define LQ(a,b) ((a)<=(b))

typedef struct {
elemtype *elem;
int length;
}table;

void bufclr(void);
status createtable1(table *);
void printtable(table);
void quicksort(table);
void Qsort(table,int,int);
int partition(table,int,int);

void main()
{
table t;
int i;
keytype key;
clrscr();
if(createtable1(&t)!=2)
exit(ERROR);
printtable(t);
quicksort(t);
printtable(t);
getch();
}

status createtable1(table *pt)
{
int i;
pt->elem=(elemtype *)malloc(sizeof(elemtype)*(MAXSIZE+1));
if(!pt->elem)
return OVERFLOW;
for(i=1;i<=1000;i++){
pt->elem[i].key=random(1000);
}
pt->length=MAXSIZE;
return OK;
}

void bufclr()
{
if(fflush(stdin))
exit(ERROR);
}

void printtable(table t)
{
int i;
printf("The current table is:\n");
for(i=1;i<t.length+1;i++){
printf("%-6d",t.elem[i].key);
if(i%10==0)
putchar('\n');
}
}

int partition(table t,int low,int high)
{
keytype pivotkey;
t.elem[0].key=t.elem[low].key;
pivotkey=t.elem[low].key;
while(low<high){
while(low<high&&t.elem[high].key>=pivotkey)
--high;
t.elem[low].key=t.elem[high].key;
while(low<high&&t.elem[low].key<=pivotkey)
++low;
t.elem[high].key=t.elem[low].key;
}
t.elem[low].key=t.elem[0].key;
return low;
}

void Qsort(table t,int low,int high)
{
int pivotloc;
if(low<high){
pivotloc=partition(t,low,high);
Qsort(t,low,pivotloc-1);
Qsort(t,pivotloc+1,high);
}
}

void quicksort(table t)
{
Qsort(t,1,t.length);
}


70,037

社区成员

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

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