2019-05-20 排序算法

2019-05-20  本文已影响0人  知成

常见排序算法模板实现


为了使用方便,整理了以下几种排序算法,没有过的文字描述以及动画图表之类的说明。

#pragma once

#define NULL 0
#define MARK -65535


template<class T>           //声明
class CDaXiongTemplateSort
{
public:
    CDaXiongTemplateSort();
    ~CDaXiongTemplateSort();
    void SetMember(T*, int );                                                   //初始化数据成员
    T*   BubbleSort(bool ascending = true);                                     //冒泡排序 默认升序
    T*   SelectionSort(bool ascending = true);                                  //选择排序
    T*   InsertionSort(bool ascending = true);                                  //插入排序
    T*   ShellSort(bool ascending = true);                                      //希尔排序
    T*   MergeSort(T*, int size, bool ascending = true);                        //归并排序 start 为起始标号,end 为结束标号
    T*   BucketSort(int n);                                                     //桶排序   n表示数的位数,只适用于正整数

protected:
    T *m_num;
    int m_length;
};

template<class T>
CDaXiongTemplateSort<T>::CDaXiongTemplateSort()
{
    this->m_num = nullptr;
}

template<class T>
CDaXiongTemplateSort<T>::~CDaXiongTemplateSort()
{
    if (nullptr != this->m_num)
    {
        delete[]this->m_num;
    }
    this->m_num = nullptr;
}

template<class T>
void CDaXiongTemplateSort<T>::SetMember(T* num, int length)
{
    this->m_num = new T[length];
    this->m_length = length;
    for (int i = 0; i < length; ++i)
    {
        this->m_num[i] = num[i];
    }
}


//冒泡
template<class T>
T* CDaXiongTemplateSort<T>::BubbleSort(bool ascending = true)
{
    
    T tempNum;
    for (int i = 0; i < this->m_length; ++i)
    {
        for (int j = 0; j < this->m_length - 1 - i; ++j)
        {
            if (ascending)
            {
                if (this->m_num[j] > this->m_num[j + 1])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[j + 1];
                    this->m_num[j + 1] = tempNum;
                }
                
            }
            else
            {
                if (this->m_num[j] < this->m_num[j + 1])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[j + 1];
                    this->m_num[j + 1] = tempNum;
                }
                
            }
            
        }
        
    }
    
    return this->m_num;
}


//选择
template<class T>
T* CDaXiongTemplateSort<T>::SelectionSort(bool ascending = true)
{
    T tempNum;
    for (int i = 0; i < this->m_length - 1; ++i)
    {
        for (int j = i + 1; j < this->m_length; ++j)
        {
            if (ascending)
            {
                if (this->m_num[i] > this->m_num[j])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[i];
                    this->m_num[i] = tempNum;
                }
                
            }
            else
            {
                if (this->m_num[i] < this->m_num[j])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[i];
                    this->m_num[i] = tempNum;
                }
                
            }

        }

    }

    return this->m_num;
}

//插入
template<class T>
T* CDaXiongTemplateSort<T>::InsertionSort(bool ascending = true)
{
    T tempNum;
    int mark;
    for (int i = 0; i < this->m_length - 1; ++i)
    {
        tempNum = this->m_num[i + 1];
        for (int j = i + 1; j > 0 ; --j)
        {
            if (ascending)
            {
                if (tempNum < this->m_num[j - 1])
                {
                    this->m_num[j] = this->m_num[j - 1];
                    this->m_num[j - 1] = tempNum;
                }
                
            }
            else
            {
                if (tempNum > this->m_num[j - 1])
                {
                    this->m_num[j] = this->m_num[j - 1];
                    this->m_num[j - 1] = tempNum;
                }
                
            }
            
            
        }
    }
    return this->m_num;
}


//希尔
template<class T>
T* CDaXiongTemplateSort<T>::ShellSort(bool ascending = true)
{
    T tempNum;
    int step = this->m_length / 2;
    while (step > 0)
    {
        for (int i = 0; i < this->m_length - step; ++i)
        {
            tempNum = this->m_num[i + step];
            for (int j = i + step; j > 0 ; j-=step)
            {
                if (ascending)
                {
                    if ((j - step) >= 0 && (tempNum < this->m_num[j - step]))       //此处若用for循环则要判别 j - srep < 0的情况
                    {
                        this->m_num[j] = this->m_num[j - step];
                        this->m_num[j - step] = tempNum;
                    }
                }
                else
                {
                    if ((j - step) >= 0 && (tempNum > this->m_num[j - step]))       //此处若用for循环则要判别 j - srep < 0的情况
                    {
                        this->m_num[j] = this->m_num[j - step];
                        this->m_num[j - step] = tempNum;
                    }
                }
                
            }
        }
        step /= 2;
    }
    return this->m_num;
}

//归并
template<class T>
T* CDaXiongTemplateSort<T>::MergeSort(T* num, int size, bool ascending = true)
{
    
    
    T* lift, *right;
    if (size >> 1)
    {
        lift = new T[size >> 1];
        right = new T[size - (size >> 1)];
        for (int i = 0, j = 0; i < size; ++i)
        {
            if (i < size >> 1)
            {
                lift[i] = num[i];
            }
            else
            {
                right[j++] = num[i];
            }
        }

        MergeSort(lift, size >> 1, ascending);          //排序lift
        MergeSort(right, size - (size >> 1), ascending);    //排序right
        /**************************************************************/

        /**************************************************************/

        int i, j, k;
        for (i = 0, j = 0, k = 0; i < size >> 1 && j < size - (size >> 1);)
        {
            if (ascending)             //升序
            {
                if (lift[i] < right[j])//合并有序
                {
                    num[k++] = lift[i++];
                }
                else
                {
                    num[k++] = right[j++];
                }
            }
            else                        //降序
            {
                if (lift[i] < right[j])//合并有序
                {
                    num[k++] = right[j++];
                }
                else
                {
                    
                    num[k++] = lift[i++];
                }
            }
            
        }
        //剩余元素全部放回去
        if (i >= size >> 1)//说明lift中的内容放完了
        {
            while (j < size - (size >> 1))
            {
                num[k++] = right[j++];
            }
        }
        else
        {
            while (i < size >> 1)
            {
                num[k++] = lift[i++];
            }
        }
        delete[] lift;
        delete[] right;
    }

    return num;
}

//桶
template<class T>
T* CDaXiongTemplateSort<T>::BucketSort(int n)
{
    T* bucket[11];                                      //声明11个桶,第11个桶用来暂存有序数据
    int temp;                                           //临时桶的下标
    static int count = 0;
    for (int i = 0;i < 11; ++i)
    {
        bucket[i] = new T[this->m_length];              //定义10 * n 的矩阵用做存储数据 n表示需要排序数组的长度
        for (int j = 0; j < this->m_length; ++j)
        {
            bucket[i][j] = MARK;                        //初始化所有桶元素
        }
    }
    
    for (int i = 0, k = 1; i < n; ++i, k *= 10)//排序的次数 n 最大值的位数  
    {
        for (int j = 0; j < this->m_length; ++j)//入桶把数组this->m_num中元素全部倒入桶中
        {
            if (this->m_num[j] < k * 10 && this->m_num[j] >= k)
            {
                temp = this->m_num[j] / k % 10;
                bucket[temp][j] = this->m_num[j];//放入桶中
            }
        }
        //立刻倒回原来的数组
        for (int m = 0, j = 0; m < 10; ++m) 
        {
            for (int x = 0; x < this->m_length; ++x)//数据的个数
            {
                if (bucket[m][x] != MARK)
                {
                    bucket[10][count++] = bucket[m][x];//倒回桶中
                    bucket[m][x] = MARK;
                }
            }
        }
    }

    for (int i = 0; i < this->m_length; ++i)
    {
        this->m_num[i] = bucket[10][i];
    }

    for (int i = 0; i < 11; ++i)
    {
        delete []bucket[i];
        bucket[i] = NULL;
    }
    return this->m_num;
}
上一篇下一篇

猜你喜欢

热点阅读