#include
#include
#include
#include
#include
//所有排序默认升序
using namespace std;
void pri(int *arr, size_t size)
{
for (size_t i = 0; i < size; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
}
//冒泡排序:每次把一个最值放到最后面,并缩小区间
void BubbleSort(int* array, size_t size)
{
assert(array);
for (size_t i = 0; i < size; ++i)
{
for (size_t j = 1; j < size - i; ++j)
{
if (array[j] < array[j - 1])
swap(array[j], array[j - 1]);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
//快排的核心思想:将比key值大的放到一边,比key值小的,放到另一边;
int QSort(int* array, size_t size, int left, int right)
{
int key = array[left];
while(left < right)
{ //key为第一个值就要从后面开始变
while (left < right&&array[right] >= key)
--right;
swap(array[left], array[right]);
while ( left < right && array[left] <= key)
++left;
swap(array[left], array[right]);
}
return left;
}
void QuickSort(int* array, size_t size, int left, int right)
{
if (leftint pivotloc = QSort(array, size, left, right);
//分别对小区和大区进行快排
QuickSort(array, size, left, pivotloc - 1);
QuickSort(array, size, pivotloc + 1, right);
}
}
void QuickSort(int* array, size_t size)
{
assert(array);
QuickSort(array, size, 0, size-1);
}
//////////////////////////////////////////////////////////////////////
template<typename T>
void merge(T array[], int low, int mid, int high)
{
int k = 0;
// 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
T* temp = new T[high - low + 1];
int lbegin = low;
int lend = mid;
int rbegin = mid + 1;
int rend = high;
//比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
for (; lbegin <= lend && rbegin <= rend; ++k)
{
if (array[lbegin] <= array[rbegin])
{
temp[k] = array[lbegin++];
}
else
{
temp[k] = array[rbegin++];
}
}
if (lbegin <= lend) // 若第一个序列有剩余,直接拷贝出来粘到合并序列尾
{
memcpy(temp + k, array + lbegin, (lend - lbegin + 1)*sizeof(T));
}
if (rbegin <= rend) // 若第二个序列有剩余,直接拷贝出来粘到合并序列尾
{
memcpy(temp + k, array + rbegin, (rend - rbegin + 1)*sizeof(T));
}
memcpy(array + low, temp, (high - low + 1)*sizeof(T));//将排序好的序列拷贝回数组中
delete[]temp;
}
template<typename T>
void merge_sort(T array[], unsigned int first, unsigned int last)
{
int mid = 0;
if (first//mid = (first+last)/2; /*注意防止溢出 */
mid = first + ((last - first) >> 1);
//mid = (first & last) + ((first ^ last) >> 1);
merge_sort(array, first, mid);
merge_sort(array, mid + 1, last);
merge(array, first, mid, last);
}
}
////////////////////LSD和MSD共用接口///////////////////////////
//获取从最低位起第n位的值
int GetValue(int key, int n)
{
return (key / (int)pow(10, n - 1)) % 10;//注意 pow函数返回double, 两个参数都是double
}
int GetBit(int *arr,size_t size)//看这个数组中最长的数有几位
{
assert(arr);
int ret = 1;
for (size_t i = 0; i < size; i++)
{
while (arr[i] / int(pow(10, ret)) > 0)
{
ret++;
}
}
return ret;
}
///////////////////////////////////////////////////////////////
//基数排序LSD十进制
void RadixSortLSD(int *arr,size_t size)
{
assert(arr);
vector<queue<int> > v1(10);//10个桶(队列)
int bit = GetBit(arr, size);
for (int i = 1; i <= bit; ++i)
{
for (size_t j = 0; j < size; ++j)
{
v1[ GetValue(arr[j], i) ].push(arr[j]);
}
for (size_t i = 0; i < size;)
{
for (int j = 0; j < 10; ++j)
{
while (!v1[j].empty())
{
arr[i] = v1[j].front();
v1[j].pop();
++i;
}
}
}
}
}
///////////////////////////////////////////
//基数排序MSD:先按最高位优先排序,再对每个最高位相同的组进行下一位的排序;
void RadixMSD(int *arr, vector<int > &tmp, size_t size, int bit)
{
vector<queue<int> > v1(10);//10个桶(队列)
for (size_t j = 0; j < size; ++j)
{
v1[GetValue(arr[j], bit)].push(arr[j]);
}
//将每个桶里的数量放入tmp中返回
for (size_t j = 0; j < 10; ++j)
{
tmp[j] = v1[j].size();
}
for (size_t i = 0; i < size;)
{
for (int j = 0; j < 10; ++j)
{
while (!v1[j].empty())
{
arr[i] = v1[j].front();
v1[j].pop();
++i;
}
}
}
}
void RadixSortMSD(int *arr, size_t size, int bit)
{
if ( bit > 0 && size > 0 )
{
vector<int> tmp(10);
RadixMSD(arr, tmp, size, bit);
for (size_t i = 0; i < 10; i++)
{
RadixSortMSD(arr, tmp[i], bit - 1);
arr += tmp[i];
}
}
return;
}
void RadixSortMSD(int *arr, size_t size)
{
assert(arr);
int bit = GetBit(arr, size);
RadixSortMSD(arr, size, bit);
}
测试函数
void test3()
{
int arr[] = { 1, 30, 21, 35, 99, 5, 21, 45, 18, 49, 25, 16, 100, 52, 8, 13 };
//BubbleSort(arr, sizeof(arr) / sizeof(int));
//pri(arr, sizeof(arr) / sizeof(int));
QuickSort(arr, sizeof(arr) / sizeof(int));
pri(arr, sizeof(arr) / sizeof(int));
}
void test4()
{
int array[] = { 1, 30, 21, 35, 99, 5, 21, 45, 18, 1, 30, 21, 35, 99, 5, 21, 45, 18, 49, 25, 16, 100, 52, 8, 13 };
pri(array, sizeof(array) / sizeof(int));
MergeSort(array, sizeof(array) / sizeof(int));
pri(array, sizeof(array) / sizeof(int));
int array1[] = { 99, 5, 21, 45, 18, 49, 21, 45, 18, 49, 25, 16, 100, 52, 8, 13 };
pri(array1, sizeof(array1) / sizeof(int));
Mergesort(array1, sizeof(array1) / sizeof(int));
pri(array1, sizeof(array1) / sizeof(int));
}
void test5()
{
int array[] = { 1, 30, 21, 35, 99, 5, 21, 10045, 18, 751, 1000, 216, 375, 25, 16, 100, 52, 8, 13 };
pri(array, sizeof(array) / sizeof(int));
RadixSortLSD(array, sizeof(array) / sizeof(int));
pri(array, sizeof(array) / sizeof(int));
int array1[] = { 3001, 3809, 2521, 9935, 2959, 5, 21, 10045, 18, 751, 1000, 216, 375, 25, 16, 100, 52, 8, 13 };
//int array1[] = { 19,18,17,15,14,13,16,12,11 };
pri(array1, sizeof(array1) / sizeof(int));
RadixSortMSD(array1, sizeof(array1) / sizeof(int));
pri(array1, sizeof(array1) / sizeof(int));
}
int main()
{
//test1();
//test2();
//test3();
//test4();
test5();
system("pause");
return 0;
}