【排序】快排/归并排序/希尔排序

//qsort
#  include  <iostream>
#  include  <algorithm>
using  namespace  std;

inline  int  findpivot(int  *A,  int  i,  int  j)
{
        return  (i  +  j)  /  2;
}

inline  int  Partition(int  *A,  int  left,  int  right,  int&  pivot)
{
        do
        {
                while(A[++left]  <  pivot);
                while((left  <  right)  &&  (pivot  <  A[--right]));
                swap(A[left],  A[right]);
        }
        while(left  <  right);
        return  left;
}

void  qsort(int  *A,  int  left,  int  right)
{
        if(right<=left) return;
        int pivotindex=findpivot(A,left,right);
        swap(A[pivotindex],A[right]);
        int k=Partition(A,left-1,right,A[right]);
        swap(A[k],A[right]);
        qsort(A,left,k-1);
        qsort(A,k+1,right);
}

int  main()
{
        int  n;
        scanf("%d",  &n);
        int  *a;
        a  =  new  int[n];

        for(int  i  =  0;  i  <  n;  i++)
                cin  >>  a[i];
        qsort(a,  0,  n  -  1);

        for(int  i  =  0;  i  <  n;  i++)
                cout  <<  a[i]  <<  "  ";
        cout  <<  endl;
        return  0;
}
//mergesort
#  include  <iostream>
#  include  <algorithm>
using  namespace  std;


void  mergesort(int  *A,  int  *temp,  int  left,  int  right)
{
    int mid=(left+right)/2;
	if (left==right) return;
	mergesort(A,temp,left,mid);
    mergesort(A,temp,mid+1,right);
    for(int i=left;i<=right;i++)
    temp[i]=A[i];
    int i1=left;
	int i2=mid+1;
    for(int curr=left;curr<=right;curr++){
	if(i1==mid+1)
    A[curr]=temp[i2++];
    else if(i2>right)
    A[curr]=temp[i1++];
    else if(temp[i1]<temp[i2])
    A[curr]=temp[i1++];
    else A[curr]=temp[i2++];
	}
}

int  main()
{
        int  n;
        scanf("%d",  &n);
        int  *a,  *temp;
        a  =  new  int[n];
        temp  =  new  int[n];
        for(int  i  =  0;  i  <  n;  i++)
                cin  >>  a[i];

        mergesort(a,  temp,  0,  n  -  1);          

        for(int  i  =  0;  i  <  n;  i++)
                cout  <<  a[i]  <<  "  ";
        cout  <<  endl;
        return  0;
}
//shellsort
#  include  <iostream>
#  include  <algorithm>
using  namespace  std;

void  inssort(int  *A,  int  n,  int  incr)
{
        for(int  i  =  incr;  i  <  n;  i  +=  incr)
                for(int  j  =  i;  (j  >=  incr)  &&  (A[j]  <  A[j  -  incr]);  j  -=  incr)
                        swap(A[j],  A[j  -  incr]);
}

void  shellsort(int  *A,  int  n)
{
        for(int i=n/3;i>2;i/=3)
           for(int j=0;j<i;j++)
             inssort(&A[j],n-j,i);
        inssort(A,n,1);
}


int  main()
{
        int  n;
        scanf("%d",  &n);
        int  *a;
        a  =  new  int[n];
        for(int  i  =  0;  i  <  n;  i++)
                cin  >>  a[i];

        shellsort(a,  n);

        for(int  i  =  0;  i  <  n;  i++)
                cout  <<  a[i]  <<  "  ";
        cout  <<  endl;
        return  0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值