实现快速排序、归并排序和基数排序算法 c++

2025-04-29 03:15:01
推荐回答(1个)
回答1:

c写的,改一下,相信聪明的楼主一定可以!
#include
#define MAX 255
int R[MAX];
int Partition(int i,int j)
{/* 调用Partition(R,low,high)时,对R[low..high]做划分,*/
/* 并返回基准记录的位置 */
int pivot=R[i]; /* 用区间的第1个记录作为基准 */
while(i while(i=pivot) /* pivot相当于在位置i上 */
j--; /* 从右向左扫描,查找第1个关键字小于pivot.key的记录R[j] */
if(i R[i++]=R[j]; /* 相当于交换R[i]和R[j],交换后i指针加1 */
while(i i++; /* 从左向右扫描,查找第1个关键字大于pivot.key的记录R[i] */
if(ipivot.key */
R[j--]=R[i]; /* 相当于交换R[i]和R[j],交换后j指针减1 */
} /* endwhile */
R[i]=pivot; /* 基准记录已被最后定位*/
return i;
} /* end of partition */

void Quick_Sort(int low,int high)
{ /* 对R[low..high]快速排序 */
int pivotpos; /* 划分后的基准记录的位置 */
if(low pivotpos=Partition(low,high); /* 对R[low..high]做划分 */
Quick_Sort(low,pivotpos-1); /* 对左区间递归排序 */
Quick_Sort(pivotpos+1,high); /* 对右区间递归排序 */
}
} /* end of Quick_Sort */

void main()
{
int i,n;
clrscr();
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and less than %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i++)
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
Quick_Sort(1,n);
puts("\nThe sequence after quick_sort is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getch();

}

2
#include
#define MAX 255
int R[MAX];

void Merge(int low,int m,int high)
{/* 将两个有序的子文件R[low..m)和R[m+1..high]归并成一个有序的 */
/* 子文件R[low..high] */
int i=low,j=m+1,p=0; /* 置初始值 */
int *R1; /* R1是局部向量,若p定义为此类型指针速度更快 */
R1=(int *)malloc((high-low+1)*sizeof(int));
if(!R1) /* 申请空间失败 */
{
puts("Insufficient memory available!");
return;
}
while(i<=m&&j<=high) /* 两子文件非空时取其小者输出到R1[p]上 */
R1[p++]=(R[i]<=R[j])?R[i++]:R[j++];
while(i<=m) /* 若第1个子文件非空,则复制剩余记录到R1中 */
R1[p++]=R[i++];
while(j<=high) /* 若第2个子文件非空,则复制剩余记录到R1中 */
R1[p++]=R[j++];
for(p=0,i=low;i<=high;p++,i++)
R[i]=R1[p];/* 归并完成后将结果复制回R[low..high] */
} /* end of Merge */

void Merge_SortDC(int low,int high)
{/* 用分治法对R[low..high]进行二路归并排序 */
int mid;
if(low {/* 区间长度大于1 */
mid=(low+high)/2; /* 分解 */
Merge_SortDC(low,mid); /* 递归地对R[low..mid]排序 */
Merge_SortDC(mid+1,high); /* 递归地对R[mid+1..high]排序 */
Merge(low,mid,high); /* 组合,将两个有序区归并为一个有序区 */
}
}/* end of Merge_SortDC */

void main()
{
int i,n;
clrscr();
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and less than %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i++)
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
Merge_SortDC(1,n);
puts("\nThe sequence after merge_sortDC is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getch();

}

3
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define MAX 5
typedef struct node
{ int k;
struct node *next;
} *lnode;
lnode my_input(int *d)
{ lnode head,temp,terminal;
char s[MAX+1];
printf("Input the records ('0' to end input):\n");
scanf("%s",s);
head=NULL;
*d=0;
terminal=NULL;
while(s[0]!='0')
{
temp=(lnode)malloc(sizeof(struct node));
if (strlen(s)>*d)
*d=strlen(s);
temp->k=atoi(s);
if (head==NULL)
{ head=temp;
terminal=temp;
}
else
{
terminal->next=temp;
terminal=temp;
}
scanf("%s",s);
}
terminal->next=NULL;

return head;
}
void my_output(lnode h)
{
lnode t=h;
printf("\n");
while (h!=NULL)
{
printf("%d ",h->k);
h=h->next;
}
h=t;
/* getch(); */
}
lnode Radix_Sort(lnode head,int d)
{
lnode p,q,h,t;
int i,j,x,radix=1;
h=(lnode)malloc(10*sizeof(struct node));
t=(lnode)malloc(10*sizeof(struct node));
for (i=d;i>=1;i--)
{
for (j=0;j<=9;j++)
{ h[j].next=NULL;
t[j].next=NULL;
}
p=head;
while (p!=NULL)
{
x=((p->k)/radix)%10;
if (h[x].next==NULL)
{
h[x].next=p;
t[x].next=p;
}
else
{
q=t[x].next;
q->next=p;
t[x].next=p;
}
p=p->next;
}

j=0;
while (h[j].next==NULL)
j++;
head=h[j].next;
q=t[j].next;
for (x=j+1;x<=9;x++)
if (h[x].next!=NULL)
{
q->next=h[x].next;
q=t[x].next;
}
q->next=NULL;
radix*=10;
printf("\n---------------------\n");
}
return head;
}
void my_free(lnode h)
{
lnode temp=h;
while (temp)
{
h=temp->next;
free(temp);
temp=h;
}
}
void main()
{
lnode h;
int d;
clrscr();
h=my_input(&d);
puts("The sequence you input is:");
my_output(h);
h=Radix_Sort(h,d);
puts("\nThe sequence after radix_sort is:");
my_output(h);
my_free(h);
puts("\n Press any key to quit...");
getch();
}


2