常用的排序算法

来源:互联网 发布:win7怎么禁止安装软件 编辑:程序博客网 时间:2024/05/21 09:55

l转自:http://blog.csdn.net/riag/article/details/1680384

 这几天, 花点时间复习了常用排序算法, 并动手编一下。这里并不解说各种排序算法,只提供代码.

/**
 * 提供常用的排序算法
 * 包括: 直接插入排序, 折半插入排序, 希尔排序,
          直接选择排序, 堆排序, 冒泡排序, 快速排序, 二路归并排序
 
*/


#ifndef NULL
#define NULL 
0
#endif


/**
 * 直接插入排序
 
*/
template
<class T>
bool InsertSort(T R[], 
int n)
{
    
if (NULL == R) return false ;

    
int i=0 ;
    
int j=0;
    T temp;

    
for(i=1; i<n; ++i)
    {
        temp 
= R[i] ;
        j
=i-1 ;
        
while(temp < R[j] && j>=0)
        {
            R[j
+1= R[j--];
            R[j
+1= temp ;
        }
    }

    
return true ;
}




/**
 * 折半插入排序
 
*/
template<class T>
bool Binary_InsertSort(T R[], int n)
{
    if (NULL == R) return false ;

    int i=0;
    int j= 0;
    T temp ;

    int low=0;
    int high = 0;
    int mid = 0;


    for(i=1; i<n; ++i)
    {
        temp = R[i] ;
        low =0 ;
        high = i-1 ;
        while(low <= high)
        {
            mid = (low+high)/2 ;
            if (temp < R[mid])
                high = mid-1 ;
            else
                low = mid +1 ;

        }

        for(j=i-1; j>=low; --j)
        {
            R[j+1] = R[j] ;

        }
        R[low] = temp ;
    }

    return true ;
}

/**
* 希尔排序
*/
template<class T>
bool ShellSort(T R[], int n)
{
    if (NULL == R) return false ;

    int i=0 ;
    int j=0 ;
    int k=0 ;
    T temp ;

    k = n/2 ;
    while(k >= 1)
    {
        for(i=k; i<n; ++i)
        {
            temp = R[i] ;
            j = i-k ;
            while(temp < R[j])
            {
                R[j+k] = R[j] ;
                j = j-k ;
            }
            R[j+k] = temp ;
        }

        k = k/2 ;
    }

    return true ;
}

/**
* 直接选择排序
*/
template<class T>
bool SelectSort(T R[], int n)
{
    if (NULL == R) return false ;

    int i=0 ;
    int j=0 ;
    int npos = 0 ;
    T temp ;

    for(i=0; i<n; ++i)
    {
        npos = i ;
        for(j=i+1; j<n; ++j)
        {
            if (R[j] < R[npos]) npos = j ;
        }

        if (npos != i)
        {
            temp = R[i] ;
            R[i] = R[npos] ;
            R[npos] = temp ;
        }
    }
    return true ;
}

/**
* 堆排序
*/
template<class T>
bool Filter(T R[], int i, int n)
{
    if (NULL == R) return false ;

    int j = 0 ;
    T temp ;

    temp = R[i] ;
    j = 2*i ;
    while(j < n)
    {
        if ( (j<n) &&(R[j]<R[j+1]) ) ++j ;
        if (temp < R[j])
        {
            R[i] = R[j] ;
            i = j ;
            j = 2*i ;
        }
        else break ;
    }

    R[i] = temp ;
    return true ;
}

template<class T>
bool HeapSort(T R[], int n)
{
    if (NULL == R) return false ;

    int i = 0;
    T temp ;

    for(i=n/2; i>=0; --i)
    {
        Filter(R, i, n) ;
    }

    for(i=n-1; i>0; --i)
    {
        temp = R[0] ;
        R[0] = R[i] ;
        R[i] = temp ;
        Filter(R, 0, i-1) ;
    }
    return true ;
}


/**
* 冒泡排序
*/
template<class T>
bool BubbleSort(T R[], int n)
{
    if (NULL == R) return false ;

    int i=0 ;
    int j=0 ;
    bool bflag = false ;
    T temp ;

    for(i=0; i<n; ++i)
    {
        bflag = true ;
        for(j=1; j<n-i; ++j)
        {
            if (R[j-1] > R[j])
            {
                bflag = false ;
                temp = R[j-1] ;
                R[j-1] = R[j] ;
                R[j] = temp ;
            }
        }
        if (bflag) break ;
    }

    return true ;
}

/**
* 快速排序
*/
template<class T>
int QuickPass(T R[], int low, int high)
{
    int down=0;
    int up = 0 ;

    down = low;
    up = high ;
    T temp = R[low] ;

    while(down < up)
    {
        while( (down<up) && (R[up]>=temp)) --up ;
        if (down < up) R[down++] = R[up] ;

        while( (down<up) && (R[down] <= temp)) ++down ;
        if (down <up) R[up--] = R[down] ;
    }

    R[down] = temp ;
    return down ;
}
template<class T>
bool QuickSort(T R[], int low, int high)
{
    if (NULL == R) return false ;

    int mid = 0 ;
    if (low < high)
    {
        mid = QuickPass(R, low, high) ;
        QuickSort(R, low, mid-1) ;
        QuickSort(R, mid+1, high) ;
    }

    return true ;
}

/**
* 二路归并排序
*/
template<class T>
void Merge(T L[], T R[], int low, int mid, int high)
{
    int i=0 ;
    int j=0 ;
    int k = 0 ;

    i=low ;
    j = mid+1;
    k = low ;

    while( (i <=mid) && (j<=high))
    {
        if (L[i] <= L[j]) R[k++] = L[i++] ;
        else R[k++] = L[j++] ;
    }

    while(i <= mid) R[k++] = L[i++] ;

    while(j <= high) R[k++] = L[j++] ;
}

template<class T>
void MergePass(T L[], T R[], int lengh, int n)
{
    int low = 0 ;
    int j = 0 ;

    low =0 ;
    while( (low+2*lengh-1) <= n)
    {
        Merge(L, R, low, low+lengh-1, low+2*lengh-1) ;
        low = low + 2*lengh ;
    }

    if (low+lengh < n)
        Merge(L, R, low, low+lengh-1, n) ;
    else
    {
        for(j=low; j<=n; ++j) R[j] = L[j] ;
    }
}

template<class T>
bool MergeSort(T L[], int n)
{
    if (NULL == L) return false ;

    int length= 0 ;
    T *R = new T[n] ;

    length = 1 ;
    while(length < n)
    {
        MergePass(L, R, length, n) ;
        length = 2*length ;
        MergePass(R, L, length, n) ;
        length = 2*length ;
    }

    return true ;
}


原创粉丝点击