排序算法

#include<stdio.h>
#define MAX 10000000
int a[100],b[100];
 
void BubbleSort(int a[],int n)
{
    int i,j,temp;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
}
void SelectSort(int a[],int n)
{
    int i,j,k,temp;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
        {
            if(a[j]<a[i])
                k=j;
        }
        if(k!=i)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        
    }
}
int partition(int a[],int p,int r)
{
    int i,j,x,temp;
    x=a[r];
    i=p;
    for(j=p;j<=r-1;j++)
    {
        if(a[j]<=x)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            i++;
        }
    }
    temp=a[i];
    a[i]=a[r];
    a[r]=temp;
    return i;
}
 
void Qsort(int a[],int p,int r)
{
    int q;
    if(p<r)
    {
        q=partition(a,p,r);
        qsort(a,p,q-1);
        qsort(a,q+1,r);
    }
}
void maxHeap(int a[],int i,int n)
{
    int l,r,largest,temp;
    l=2*i+1;
    r=2*i+2;
    if(l<n&&a[l]>a[i])
        largest=l;
    else
        largest=i;
    if(r<n&&a[r]>a[largest])
        largest=r;
    if(largest!=i)
    {
        temp=a[i];
        a[i]=a[largest];
        a[largest]=temp;
        maxHeap(a,largest,n);
    }
    
    
}
void buildMaxHeap(int a[],int n)
{
    int i;
    for(i=n/2;i>=0;i--)
        maxHeap(a,i,n);
 
}
void HeapSort(int a[],int n)
{
    int i,temp,t;
    buildMaxHeap(a,n);
    t=n;
    for(i=n-1;i>=1;i--)
    {
        temp=a[i];
        a[i]=a[0];
        a[0]=temp;
        t--;
        maxHeap(a,0,t);
    }
}
void merge(int a[],int p,int q,int r)
{
    int i,j,k,c1,d1,c[100],d[100];  
    c1=q-p+1;
    d1=r-q;
    //printf("%d %d",c1,d1);
    for(i=0;i<c1;i++)
        c[i]=a[p+i];
    for(i=0;i<d1;i++)
        d[i]=a[q+1+i];
    c[c1]=MAX;
    d[d1]=MAX;
    i=0;j=0;
    for(k=p;k<=r;k++)
    {
        if(c[i]<=d[j])
        {
            a[k]=c[i];
            i++;
        }
        else
        {
            a[k]=d[j];
            j++;
        }
    }
}
 
 
void MergeSort(int a[],int p,int r)
{
    int q;
    if(p<r)
    {
        q=(p+r)/2;
        mergesort(a,p,q);
        mergesort(a,q+1,r);
        merge(a,p,q,r);
    }
}
void CountingSort(int a[],int k,int n)//a[i]<=k
{
    int i,c[100];
    for(i=0;i<k;i++)
        c[i]=0;
    for(i=0;i<n;i++)
        c[a[i]]++;
    for(i=1;i<k;i++)
        c[i]=c[i]+c[i-1];
    for(i=n-1;i>=0;i--)
    {
        b[c[a[i]]-1]=a[i];
        c[a[i]]--;
    }
    for(i=0;i<n;i++)
        a[i]=b[i];
}
 
 
 
    
 
 
 
void main()
{
    int i,n,max;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
//    BubbleSort(a,n);
//    SelectSort(a,n);
//    Qsort(a,0,n-1);
//    HeapSort(a,n);
//    MergeSort(a,0,n-1);
//    max=100;//a[i]均为0--max的数才能用此方法
//    CountingSort(a,max,n);
    for(i=0;i<n;i++)
        printf("%d ",a[i]);
    printf("/n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值