内排序方法总结

#include <bits/stdc++.h>

using namespace std;

typedef int ElementType;


/*插入排序*/ /*时间复杂度O(N²)*/
void InsertionSort ( ElementType *A, int N )
{
    int j, P;

    ElementType Tmp;
    for( P=1; P<N; P++)
    {
        Tmp = A[P];
        for( j=P; j>0 && A[j-1]>Tmp; j-- )
            A[j] = A[j-1];
        A[j] = Tmp;
    }
}



/*希尔排序*/ ///特殊的插入排序
void Shallsort ( ElementType *A, int N )
{
    int i, j, Increment;
    ElementType Tmp;

    for( Increment=N/2; Increment>0; Increment/=2)
    {
        for( i=Increment; i<N; i++ )
        {
            Tmp = A[i];
            for( j=i; j>=Increment && Tmp < A[j - Increment]; j-=Increment )
                A[j] = A[j - Increment];
            A[j] = Tmp;
        }
    }
}


/*堆排序*/
#define LeftChild( i ) ( 2 * ( i ) + 1 )

void PercDown ( ElementType *A, int i, int N )
{
    int Child;
    ElementType Tmp;

    for( Tmp = A[i]; LeftChild(i) < N; i = Child )
    {
        Child = LeftChild(i);
        if( Child != N-1 && A[Child + 1] > A[Child] )
            Child++;
        if( Tmp < A[Child] )
            A[i] = A[Child];
        else break;
    }
    A[i] = Tmp;
}
void Heapsort( ElementType *A, int N )
{
    int i;
    for( i = N/2; i >= 0; i-- )
        PercDown( A, i, N);
    for( i = N-1; i >= 0; i-- )
    {
        swap( A[0], A[i] );
        PercDown( A, 0, i );
    }

}


/*快速排序*/

void qusort( int *a, int lt, int rt )
{
    if(lt >= rt) return;
    int i = lt, j = rt, key = a[lt];
    while(i<j)
    {
        while(i<j && a[j] >= key)
            j--;
        a[i] = a[j];
        while(i<j && a[i] <= key)
            i++;
        a[j] = a[i];
    }

    a[i] = key;
    qusort( a, lt, i-1 );
    qusort( a, i+1, rt );
}

void qsort(void *base, size_t nmemb, size_t size, 
                             int (*compar)(const void*, const void*))
          //目标数组,    项数,     sizeof(int), 函数指针
int mycomp ( const void *p1, const void *p2 )
{
    /*使用指向int类型的指针访问值*/
    const int *a1 = (const int *) p1;
    const int *a2 = (const int *) p2;
    if( *a1 < *a2 )            /*从小到大排序*/
        return -1;
    else if( *a1 == *a2 )
        return 0;
    else 
        return 1;
}




/*归并排序*/
/* Lpos = start of left half, Rpos = start of right half */
void Merge ( ElementType *A, ElementType *TmpArray,
                  int Lpos, int Rpos, int RightEnd )
{
    int i, LeftEnd, NumElements, TmpPos;
    
    LeftEnd = Rpos - 1;
    TmpPos  = Lpos;
    NumElements = RightEnd - Lpos + 1;
    
    /* main loop */
    while( Lpos <= LeftEnd && Rpos <= RightEnd )
    {
        if( A[Lpos] <= A[Rpos] )
            TmpArray[TmpPos++] = A[Lpos++];  /* Copy rest of first  half */
        else 
            TmpArray[TmpPos++] = A[Rpos++];  /* Copy rest of second half */
    }
    while( Lpos <= LeftEnd  )
        TmpArray[TmpPos++] = A[Lpos++];
    while( Rpos <= RightEnd )
        TmpArray[TmpPos++] = A[Rpos++];
    
    /* Copy TmpArray back */
    for( i=0; i<NumElements; i++, RightEnd-- )
        A[RightEnd] = TmpArray[RightEnd];
}
void Msort ( ElementType *A, ElementType *TmpArray,
                                 int Left, int Rigth )
{
    int Center;

    if( Left < Right )
    {
        Center = ( Left + Right ) / 2;
        Msort( A, TmpArray, Left, Center );
        Msort( A, TmpArray, Center+1, Right );
        Merge( A, TmpArray, Left, Center+1; Right );
    }
}
void Mergesort ( ElementType *A, int N )
{
    ElementType *TmpArray;
    
    TmpArray = malloc( N * sizeof( ElementType ));
    if( !TmpArray )
    {
        Msort( A, TmpArray, 0, N-1 );
        free( TmpArray );
    }
    else
    //   FatalError( "No space for tmp array!!!" );
        return;
}


/*选择排序*/
//略
/*桶排序*/
//略
/*冒泡排序*/
//略


int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值