堆排序
#include<stdio.h>
#define SWAP(X,Y) {X=X+Y;Y=X-Y;X=X-Y;}
int myputs(int *array,int num)
{
int i;
for(i=0;i<num;i++)
{
printf("%3d ",array[i]);
if(!((i+1)%20))printf("\n");
}
printf("\n");
return 0;
}
int AdjustHeap_1(int *array,int num,int beg)
{
int i,end=num-1;
if(!((num-beg)%2))
{
if(array[end]<array[(end-beg-1)/2+beg])
{
SWAP(array[end],array[(end-beg-1)/2+beg])
}
--end;
}
for(i=end;i>beg+1;i-=2)
{
if(array[i-1]<array[(i-beg)/2-1+beg])
{
SWAP(array[i-1],array[(i-beg)/2-1+beg])
}
if(array[i]<array[(i-beg)/2-1+beg])
{
SWAP(array[i],array[(i-beg)/2-1+beg])
}
}
return 0;
}
int HeapSort_1(int *array,int num)
{
int i;
for(i=0;i<num-1;i++)
{
AdjustHeap_1(array,num,i);
}
return 0;
}
int AdjustHeap_2(int *array,int num,int beg)
{
int i,end=num-1;
if(!((num-beg)%2))
{
if(array[end]>array[(end-beg-1)/2+beg])
{
SWAP(array[end],array[(end-beg-1)/2+beg])
}
--end;
}
for(i=end;i>beg+1;i-=2)
{
if(array[i-1]>array[(i-beg)/2-1+beg])
{
SWAP(array[i-1],array[(i-beg)/2-1+beg])
}
if(array[i]>array[(i-beg)/2-1+beg])
{
SWAP(array[i],array[(i-beg)/2-1+beg])
}
}
return 0;
}
int HeapSort_2(int *array,int num)
{
int i;
for(i=0;i<num-1;i++)
{
AdjustHeap_2(array,num,i);
}
return 0;
}
int test(int (*p)(int *,int),char *pr,int *array,int num)
{
printf("****************************堆排序函数%s ***************************\n",pr);
printf("排序前:\n");
myputs(array,num);
(*p)(array,num);
printf("排序后:\n");
myputs(array,num);
return 0;
}
int main()
{
int array1[]={
3, 5, 9,88, 7,53,87, 4,38, 1,65,75,46,25,93, 6, 2,79,12, 4,
35,59,98,87,75,58,84,43,31,16,67,74,42,29,96,62,27,71,14,43,
30,50,90,88,70,35,78,40,83,10,56,57,46,52,93,60,20,97,21,40,
53,95,89,78,57,85,48,34,13,61,76,47,24,92,69,26,72,17,41,34,
};
int array2[]={
3, 5, 9,88, 7,53,87, 4,38, 1,65,75,46,25,93, 6, 2,79,12, 4,
35,59,98,87,75,58,84,43,31,16,67,74,42,29,96,62,27,71,14,43,
30,50,90,88,70,35,78,40,83,10,56,57,46,52,93,60,20,97,21,40,
53,95,89,78,57,85,48,34,13,61,76,47,24,92,69,26,72,17,41,34,
};
test(HeapSort_1,"HeapSort_1",array1,sizeof(array1)/sizeof(int));
test(HeapSort_2,"HeapSort_2",array2,sizeof(array2)/sizeof(int));
return 0;
}
归并排序
#include<stdio.h>
#include<stdlib.h>
#define SWAP(X,Y) {X=X+Y;Y=X-Y;X=X-Y;}
int myputs(int *array,int num)
{
int i;
for(i=0;i<num;i++)
{
printf("%3d ",array[i]);
if(!((i+1)%20))printf("\n");
}
printf("\n");
return 0;
}
int Merge(int *array,int beg1,int num1,int beg2,int num2)
{
int *p=(int *)malloc((num1+num2)*sizeof(int));
int *tmp=p;
int i=beg1,j=beg2,k=0;
while((i<(beg1+num1))&&(j<(beg2+num2)))
{
if(array[i]<=array[j])
*tmp++=array[i++];
else
*tmp++=array[j++];
}
if((beg1+num1)==i)
{
while(j<(beg2+num2))
*tmp++=array[j++];
}
else if((beg2+num2)==j)
{
while(i<(beg1+num1))
*tmp++=array[i++];
}
for(k=0;k<num1+num2;k++)
array[beg1+k]=p[k];
free(p);
return 0;
}
int MergeSort_1(int *array,int beg,int num)
{
if(num>2)
{
MergeSort_1(array,beg,num/2);
MergeSort_1(array,beg+num/2,num-num/2);
Merge(array,beg,num/2,beg+num/2,num-num/2);
}
else if(2==num)
{
if(array[beg]>array[beg+1])
{
SWAP(array[beg],array[beg+1])
}
}
return 0;
}
int test(int (*p)(int *,int,int),char *pr,int *array,int beg,int num)
{
printf("****************************归并排序函数%s ***************************\n",pr);
printf("排序前:\n");
myputs(array,num);
(*p)(array,beg,num);
printf("排序后:\n");
myputs(array,num);
return 0;
}
int main()
{
int array1[]={
3, 5, 9,88, 7,53,87, 4,38, 1,65,75,46,25,93, 6, 2,79,12, 4,
35,59,98,87,75,58,84,43,31,16,67,74,42,29,96,62,27,71,14,43,
30,50,90,88,70,35,78,40,83,10,56,57,46,52,93,60,20,97,21,40,
53,95,89,78,57,85,48,34,13,61,76,47,24,92,69,26,72,17,41,34,
};
test(MergeSort_1,"MergeSort_1",array1,0,sizeof(array1)/sizeof(int));
return 0;
}