排序算法C++实现 理论部分自己理解

/////交换两个数据
template <typename T> void Swap(T &a,T &b) { a=a^b; b=a^b; a=a^b; }
template <typename T>
void prin(T Arr[], int len)
{
    for (int i=0; i<len; i++)
    {
        cout<<Arr[i]<<"  ";
    }
    cout<<endl;
}

/////直接插入排序
template <typename T>
void InsertSort(T* pt, int len)
{
    if ((pt == NULL) || (len <=0))
    {
        cout<<"input parameter error"<<endl;
        return;
    }
    for (int i=0; i<len-1 ; i++)//////需要比较测试:len-1次,即[0, len-1)
    {
        for (int j=i+1; j>=1; j--)
        {
            if (pt[j-1] >pt[j])
                Swap(pt[j-1], pt[j]);
        }
    }
}
/////shell排序是直接插入排序的改进
template <typename T>
void shell(T* Arr, int len, int incr)
{
    if ((Arr == NULL) || (len<=0) ||(incr<=0))
    {
        return;
    }
    int i=0;
    for ( i=0; i < len-incr; i++)
    {
        ////i = [0-incr),倍数相邻的两个元素比较排序.
        ////i = [incr, 2*incr)倍数相邻的三个元素排序
     ////依次类推
   for (int j = i+incr; j>=incr; j-=incr) { if (Arr[j-incr] >Arr[j] ) Swap(Arr[j-incr], Arr[j]); } } }
template
<typename T> void ShellSort(T* Arr, int len) { if (Arr = NULL || len <=0) return; int incr = len; do { incr = incr/3+1; shell<T>(Arr, len, incr); } while (incr >1); }

template <typename T>
void bubbleSort(T* Arr, int len)
{
    bool exchang = false;
    for (int i=0; i<len-1; i++)
    {
        exchang = false;
        for (int j=0 ; j<len-1-i; j++)
        {
            if (Arr[j] > Arr[j+1])
            {
                exchang = true;
                Swap(Arr[j], Arr[j+1]);
            }
        }
        if (!exchang)
            break;
    }
}
template <typename T>
void SelectSort(T* Arr, int len)
{
    for (int i=0; i<len; i++)
    {
        int k= i;
        for (int j=i+1; j<len; j++)
        {
            if (Arr[k] >Arr[j])
                k = j;
        }
        Swap(Arr[k], Arr[i]);
    }
}

/////创建堆  
////数组的非降序排列  大根堆
////数组的非升序排列  小根堆
template <typename T>
void HeapAdjust(T* Arr, int s, int length)
{
    T temp = Arr[s];
    //s代表带调整的根节点
    int child = 2*s+1;
    while (child< length)
    {
        //创建小堆  非升序排列
        if (child+1<length && Arr[child + 1]   < Arr[child])
        {
            ++child;/////child为左右孩子中值较小的孩子的位置
        }
        if (Arr[child] < Arr[s])
        {
            Arr[s] = Arr[child];
            s= child;
            child = 2*s+1;
        }
        else
            break;
        Arr[s] = temp;
    }
}
template <typename T>
void BuildingHeap(T H[], int length)  ///length 下标的位置
{   
    //最后一个有孩子结点的节点位置 i=  length/ 2  -1
    ///从下到上,一次筛选,构建堆
    for (int i = length / 2 -1; i >= 0; --i)  
        HeapAdjust(H,i,length);  

}  
template <typename T>
void HeapSort(T* Arr, int len)
{
    BuildingHeap(Arr, len);
    cout<<"BuildHeap:"<<endl;
    prin(Arr, len);
    ////堆顶元素和堆中最后一个元素交换之后,对堆进行调整
    for (int i= len -1; i>0; --i)
    {
        Swap<T>(Arr[i], Arr[0]);
         /*T temp = Arr[i]; Arr[i] = Arr[0]; Arr[0] = temp; */////////////////
        HeapAdjust<T>(Arr, 0, i);
    }
}


template <typename T>
void QuickSort(T* Arr, int left, int right)
{
    if (left >right)
        return;
    int low = left;
    int high = right;
    T temp = Arr[left];
    while(low < high)
    {
        while((low < high)&& (Arr[high] >= temp))
            high--;
        while((low < high)&& (Arr[low] <= temp))
            low++;
        if (low <high)
            Swap(Arr[low], Arr[high]);
    }
    Arr[left] = Arr[low];
    Arr[low] = temp;

    QuickSort(Arr, left, low-1);
    QuickSort(Arr, low+1, right);
}
template <typename T>
void Merge(T* Arr, int st, int md, int ed, T* temp)
{
    int s= st;  int m = md;   int e = ed;
    int mm = md+1;
    int index = 0;
    while (s<=m && mm <=e)
    {
        if (Arr[s] < Arr[mm])
            temp[index++] = Arr[s++];
        if (Arr[mm] < Arr[s])
            temp[index++] = Arr[mm++];
    }
    while (s<=m)
        temp[index++] = Arr[s++];
    while (mm<=e)
        temp[index++] = Arr[mm++];
    for (int i=0; i<index; i++)
        Arr[st+i] = temp[i];
}
template <typename T>
void MgerSort(T* Arr, int st, int ed, T* temp)
{
    if (Arr == NULL ||st<0 || ed <0 || temp == NULL)
    {
        return ;
    }
    if (st < ed)
    {
        int md = (st+ed)/2;
        MgerSort(Arr, st, md, temp );
        MgerSort(Arr, md+1, ed, temp);
        Merge(Arr, st, md, ed, temp);
    }
}

 

posted @ 2015-05-16 11:07  niupan369  阅读(168)  评论(0编辑  收藏  举报