C语言实现冒泡排序,选择排序和插入排序
冒泡排序
#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 BubbleSort_1(int *array,int num)
{
int i=0,j=0,flag=1;
for(i=0;i<num;i++)
{
if(flag)
{
flag=0;
for(j=0;j<num-1-i;j++)
{
if(*(array+j)>*(array+j+1))
{
SWAP(*(array+j),*(array+j+1))
flag=1;
}
}
}
else break;
}
return 0;
}
/*冒泡排序(从后往前)*/
int BubbleSort_2(int *array,int num)
{
int i=0,j=0,flag=1;
for(i=0;i<num;i++)
{
if(flag)
{
flag=0;
for(j=num-2;j>=i;j--)
{
if(*(array+j)>*(array+j+1))
{
SWAP(*(array+j),*(array+j+1))
flag=1;
}
}
}
else break;
}
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(BubbleSort_1,"BubbleSort_1",array1,sizeof(array1)/sizeof(int));
test(BubbleSort_2,"BubbleSort_2",array2,sizeof(array2)/sizeof(int));
return 0;
}
简单选择排序
#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 SelectSort_1(int *array,int num)
{
int i=0,j=0,min;
for(i=0;i<num;i++)
{
min=i;
for(j=i+1;j<num;j++)
if(array[min]>array[j])
min=j;
if(i!=min)
{
SWAP(array[i],array[min])
}
}
return 0;
}
/*选择排序(从后往前选择)*/
int SelectSort_2(int *array,int num)
{
int i=0,j=0,max;
for(i=num-1;i>0;i--)
{
max=i;
for(j=0;j<i;j++)
if(array[max]<array[j])
max=j;
if(i!=max)
{
SWAP(array[i],array[max])
}
}
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(SelectSort_1,"SelectSort_1",array1,sizeof(array1)/sizeof(int));
test(SelectSort_2,"SelectSort_2",array2,sizeof(array2)/sizeof(int));
return 0;
}
插入排序
#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 InsertSort_1(int *array,int num)
{
int i=0,j=0,k=0,tmp=0;
for(i=1;i<num;i++)
{
for(j=0;j<i;j++)
if(*(array+i)<*(array+j))
{
tmp=*(array+i);
for(k=i-1;k>=j;k--)
*(array+k+1)=*(array+k);
*(array+j)=tmp;
break;
}
}
return 0;
}
/*插入排序*/
int InsertSort_2(int *array,int num)
{
int i=0,j=0,max=num,tmp=0;
while(max--)
{
tmp=*(array+max-1);
for(i=num-1;i>=max;i--)
if(tmp>*(array+i))
{
for(j=max-1;j<=i-1;j++)
*(array+j)=*(array+j+1);
*(array+i)=tmp;
break;
}
}
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(InsertSort_1,"InsertSort_1",array1,sizeof(array1)/sizeof(int));
test(InsertSort_2,"InsertSort_2",array2,sizeof(array2)/sizeof(int));
return 0;
}