欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

排序算法模板实现

程序员文章站 2023-12-06 13:02:16
常见的排序算法模板实现 学过的东西总是很容易忘记,最近用模板整理 了常见的排序算法。涉及模板的知识在这里我就不细细的介绍了。 ......

常见的排序算法模板实现

学过的东西总是很容易忘记,最近用模板整理 了常见的排序算法。涉及模板的知识在这里我就不细细的介绍了。

#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;
}