三种排序算法及三种变量交换的宏实现

1、快速排序

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define N 100

#define SWAP(x,y)                    \

{                                               \

       int t;t=(x);(x)=(y);(y)=t;    \

}

 

 

int partition(int a[],int l,int r);

void quick_sort(int a[],int l,int r);

 

 

int main(int argc,char *argv[])

{

       int a[N],n,i;

       memset(a,0,sizeof(a));

       printf("Enter how many numbers \n");

       scanf("%d",&n);

       for(i=0;i<n;i++)

              scanf("%d",a+i);

       printf("before sorted:\n");

       for(i=0;i<n;i++)

              printf("%d\t",a[i]);

       printf("\n");

       quick_sort(a,0,n-1);

       printf("after sorted:\n");

       for(i=0;i<n;i++)

              printf("%d\t",a[i]);

       printf("\n");

       system("pause");

       return 0;

}

void quick_sort(int a[],int l,int r)

{

       int i;

       if(r<=l)return;

       i=partition(a,l,r);

       quick_sort(a,l,i-1);

       quick_sort(a,i+1,r);

       return ;

}

 

 

int partition(int a[],int l,int r)

{

       int i,j,v;

       i=l-1;

       j=r;

       v=a[r];

       for(;;)

       {

              while(a[++i]<v);

              while(v<a[--j])if(j==l)break;

              if(j<=i)break;

              SWAP(a[i],a[j]);

       }

       SWAP(a[i],a[r]);

       return i;

}

2、冒泡排序

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

#define SWAP(a, b)  {(a) ^= (b); (b) ^= (a); (a) ^= (b);}

 

 

int main()

{

       int num,a[N],i,j;

       memset(a,0,sizeof(a));

       printf("Enter  how many elements in the array?\n");

       scanf("%d",&num);

       for(i=0;i<num;i++)

               scanf("%d",a+i);

       printf("before sorted:\n");

       for(i=0;i<num;i++)

               printf("%d\t",a[i]);

       printf("\n");

       for(i=num-1;i>0;i--)

                     for(j=0;j<i;j++)

                     {

                if(a[j]>a[j+1])

                                   SWAP(a[j],a[j+1]);

            }

       printf("after sorted:\n");

       for(i=0;i<num;i++)

               printf("%d\t",a[i]);

       printf("\n");

          system("pause");

       return 0;

}

3、选择排序

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))

 

 

int a[N],n=0;

 

 

void sort_a();

void p_a();

 

 

int main()

{

       int i=0;

       memset(a,0,sizeof(a));

       printf("How many int numbers?\n");

       scanf("%d",&n);

       if(n<1 || n>N)

       {

              printf("can't accept\n");

              return 1;

       }

       else

       {

              for(i=0;i<n;i++)

                     scanf("%d",a+i);

       }

       printf("before sorted:\n");

       p_a();

       sort_a();

       printf("after sorted\n");

       p_a();

       system("pause");

       return 0;

}

 

 

void p_a()

{

       int i=0;

       for(i=0;i<n-1;i++)

              printf("%d\t",a[i]);            

       printf("%d\n",a[i]);

}

 

 

void sort_a()

{

       int i,j,min,temp;

       for(i=0;i<n-1;i++)

       {

              min=i;

              for(j=i;j<n;j++)

              {

                     if(a[j]<a[min])

                            min=j;

              }

              if(min!=i)

                     SWAP(a[min],a[i],temp);

       }

}

--------------------------------------------

简单测试结果:
How many int numbers?
6
13      7       19      5       29      31
before sorted:
13      7       19      5       29      31
after sorted
5       7       13      19      29      31
请按任意键继续. . .


posted @ 2005-05-10 10:57  OOP  阅读(418)  评论(0编辑  收藏  举报