首页 > 代码库 > 内部排序实现(数据结构)
内部排序实现(数据结构)
以下源码全部经过测试,有些地方可能不是最优,仅是练习。如果错误或更好的方法欢迎大家指出。以下仅供参考:
说明:为了测试的方便,在每个排序算法前都进行了一次初始化-重新分配内存,以防止影响后面的测试。cygwin下G++测试通过
#include <cstdio>
#include <cmath>
#include <queue>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
/*
*根据内部使用的原则:插入排序,选择排序,交换排序,
*归并排序,基数排序
*
*/
/*
* 插入排序
*/
//常规插入
void InsertSort(int* arr,unsigned int len);
//二分插入
void BinInsertSort(int* arr,unsigned int len);
//2路插入
void TwoWayInsertSort(int* arr,unsigned int len);
void TwoWayBinInsertSort(int* arr,unsigned int len);
//表插入
void TableInsertSort(int* arr,unsigned int len);
//希尔排序
void ShellSort(int* arr,unsigned int len,int* delta,unsigned int deltaLen);
/*
* 交换排序
*/
//冒泡排序
void BubbleSort(int* arr,unsigned int len);
//快速排序
void QuickSort(int* arr,unsigned int len);
/*
* 选择排序
*/
//简单选择排序
void SimpleSelectionSort(int* arr,unsigned int len);
//树形选择排序
void TreeSelectionSort(int* arr,unsigned int len);
//堆排序
void HeapSelectionSort(int* arr,unsigned int len);
/*
* 归并排序
*/
//2路归并排序(递归)
void TwoWayMergeSortRer(int* arr,unsigned int len);
void TwoWayMergeSortRer2(int* arr,unsigned int len);
//2路归并排序(非递归)
void TwoWayMergeSort(int* arr,unsigned int len);
/*
* 基数排序
*/
void RadixSort(int* arr,unsigned int len);
//输出数组内容
void PrintArray(int* arr,unsigned int len)
{
if(!arr) return ;
for(int i = 0; i < len; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;
}
int main(int argc,char* argv[])
{
int arr[] = {1,2,4,3,10,6,8,7,4,98,8,24};
unsigned int len = sizeof(arr)/sizeof(arr[0]);
std::cout << "原始顺序:" << std::endl;
PrintArray(arr,len);
std::cout << "插入排序:" << std::endl;
std::cout << "普通插入排序:" << std::endl;
InsertSort(arr,len);
std::cout << "二分插入排序:" << std::endl;
BinInsertSort(arr,len);
std::cout << "2路插入排序:" << std::endl;
TwoWayInsertSort(arr,len);
std::cout << "2路插入排序(二分):" << std::endl;
TwoWayBinInsertSort(arr,len);
std::cout << "表插入排序:" << std::endl;
TableInsertSort(arr,len);
std::cout << "希尔排序:" << std::endl;
int delta[3] = {5,3,1};
ShellSort(arr,len,delta,3);
std::cout << std::endl;
std::cout << "交换排序:" << std::endl;
std::cout << "起泡排序:" << std::endl;
BubbleSort(arr,len);
std::cout << "快速排序:" << std::endl;
QuickSort(arr,len);
std::cout << std::endl;
std::cout << "选择排序:" << std::endl;
std::cout << "简单选择排序:" << std::endl;
SimpleSelectionSort(arr,len);
std::cout << "树形选择排序:" << std::endl;
TreeSelectionSort(arr,len);
std::cout << "堆选择排序:" << std::endl;
HeapSelectionSort(arr,len);
std::cout << std::endl;
std::cout << "归并排序:" << std::endl;
std::cout << "2路归并排序(递归):" << std::endl;
TwoWayMergeSortRer(arr,len);
std::cout << "2路归并排序(递归)2:" << std::endl;
TwoWayMergeSortRer2(arr,len);
std::cout << "2路归并排序(非递归):" << std::endl;
TwoWayMergeSort(arr,len);
std::cout << std::endl;
std::cout << "基数排序:" << std::endl;
RadixSort(arr,len);
return 0;
}
/*
* 插入排序
*/
//常规插入
void InsertSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//开始排序
for(int i = 2; i <= len; ++i)
{
if(tmpArr[i] < tmpArr[i - 1])
{
tmpArr[0] = tmpArr[i];
tmpArr[i] = tmpArr[i - 1];
int j = 0;
for(j = i - 2; tmpArr[0] < tmpArr[j]; --j)
tmpArr[j + 1] = tmpArr[j];
tmpArr[j + 1] = tmpArr[0];
}
}
//输出
PrintArray(tmpArr + 1,len);
free(tmpArr);
}
//二分插入,仅仅减少了比较次数,移动次数不变
void BinInsertSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//二分插入排序
for(int i = 2; i <= len; ++i)
{
if(tmpArr[i] < tmpArr[i - 1])
{
tmpArr[0] = tmpArr[i];
int low = 1, high = i - 1;
while(low <= high)
{
int mid = low + (high - low) / 2;
if(tmpArr[0] < tmpArr[mid]) high = mid - 1;
else low = mid + 1;//这是保持排序的稳定性
}
for(int j = i - 1; j >= low; --j)
tmpArr[j + 1] = tmpArr[j];
tmpArr[low] = tmpArr[0];
}
}
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr);
}
//2路插入,在折半插入排序的基础上改进,目的是减少排序过程中
//移动次数,但需要n个记录的辅助存储空间
void TwoWayInsertSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//2路插入
int* tmpArr2 = (int*)malloc(sizeof(int) * len);
if(!tmpArr2)
{
std::cout << "辅助空间内存分配失败!" << std::endl;
return ;
}
memcpy((char*)tmpArr2,(char*)arr,sizeof(int) * len);
unsigned int first = len,last = 1;
tmpArr2[0] = tmpArr[1];
for(int i = 2; i <= len; ++i)
{
if(tmpArr[i] < tmpArr2[0])
{
int j = first;
while(j < len && tmpArr2[j] <= tmpArr[i])
{
tmpArr2[j - 1] = tmpArr2[j];
++j;
}
tmpArr2[j - 1] = tmpArr[i];
first -= 1;
}
else
{
int j = last;
while(j - 1 > 0 && tmpArr2[j - 1] > tmpArr[i])
{
tmpArr2[j] = tmpArr2[j - 1];
--j;
}
tmpArr2[j] = tmpArr[i];
last += 1;
}
}
memcpy((char*)tmpArr + sizeof(int),(char*)tmpArr2,sizeof(int) * len);
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr2);
free(tmpArr);
}
void TwoWayBinInsertSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//2路插入
int* tmpArr2 = (int*)malloc(sizeof(int) * len);
if(!tmpArr2)
{
std::cout << "辅助空间内存分配失败!" << std::endl;
return ;
}
memcpy((char*)tmpArr2,(char*)arr,sizeof(int) * len);
unsigned int first = len,last = 1;
tmpArr2[0] = tmpArr[1];
for(int i = 2; i <= len; ++i)
{
if(tmpArr[i] < tmpArr2[0])
{
int low = first,high = len - 1;
while(low <= high)
{
int mid = low + (high - low) / 2;
if(tmpArr[i] < tmpArr2[mid]) high = mid - 1;
else low = mid + 1;
}
for(int j = first; j < low; ++j) tmpArr2[j] = tmpArr2[j + 1];
tmpArr2[low] = tmpArr[i];
first -= 1;
}
else
{
int low = 1,high = last - 1;
while(low <= high)
{
int mid = low + (high - low) / 2;
if(tmpArr[i] < tmpArr2[mid]) high = mid - 1;
else low = mid + 1;
}
for(int j = last - 1; j >= low; --j) tmpArr2[j + 1] = tmpArr2[j];
tmpArr2[low] = tmpArr[i];
last += 1;
}
}
memcpy((char*)tmpArr + sizeof(int),(char*)tmpArr2,sizeof(int) * len);
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr2);
free(tmpArr);
}
//表节点
typedef struct tag_TableSortNode
{
int key;
unsigned int next;
}TableSortNode;
typedef struct tag_TableSeq
{
TableSortNode *tsn;
unsigned int len;
}TableSeq;
//表插入
void TableInsertSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
TableSeq seq = {0,0};
seq.len = len + 1;
seq.tsn = (TableSortNode*)malloc(sizeof(TableSortNode) * seq.len);
if(!seq.tsn)
{
std::cout << "辅助内存分配失败!" << std::endl;
return ;
}
for(int i = 0; i < len; ++i)
{
seq.tsn[i + 1].key = arr[i];
seq.tsn[i + 1].next = 0;
}
seq.tsn[0].key = len;
seq.tsn[0].next = 1;
std::cout << "{" << std::endl;
//构建循环链表
for(int i = 2; i <= len; ++i)
{
unsigned int j = seq.tsn[0].next;
unsigned int pre = 0;
while(j && seq.tsn[j].key <= seq.tsn[i].key)
{
pre = j;
j = seq.tsn[j].next;
}
seq.tsn[pre].next = i;
seq.tsn[i].next = j;
}
{
std::cout << "链表输出:" << std::endl;
int j = seq.tsn[0].next;
while(j)
{
std::cout << seq.tsn[j].key << " ";
j = seq.tsn[j].next;
}
std::cout << std::endl;
}
//重排
{
std::cout << "重排后:" << std::endl;
int j = seq.tsn[0].next;
for(int i = 1; i < seq.len; ++i)
{
while(j < i) j = seq.tsn[j].next;
int q = seq.tsn[j].next;
if(j != i)
{
TableSortNode tsn = seq.tsn[j];
seq.tsn[j] = seq.tsn[i];
seq.tsn[i] = tsn;
seq.tsn[i].next = j;
}
j = q;
}
}
//输出数组
for(int i = 1; i < seq.len; ++i)
{
std::cout << seq.tsn[i].key << " ";
}
std::cout << std::endl;
std::cout << "}" << std::endl;
free(seq.tsn);
}
//希尔排序
void ShellInsert(int* arr,int len,int delta)
{
for(int i = delta + 1; i <= len; ++i)
{
if(arr[i] < arr[i - delta])
{
arr[0] = arr[i];
int j = 0;
for(j = i - delta; j > 0 && arr[0] < arr[j]; j -= delta)
arr[j + delta] = arr[j];
arr[j + delta] = arr[0];
}
}
}
void ShellSort(int* arr,unsigned int len,int* delta,unsigned int deltaLen)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//排序
for(unsigned int k = 0; k < deltaLen; ++k)
ShellInsert(tmpArr,len,delta[k]);
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr);
}
/*
* 交换排序
*/
//冒泡排序
void BubbleSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//排序
for(unsigned int i = len + 1; i > 0; --i)
{
int flag = 0;
for(unsigned int j = 1; j < i; ++j)
{
if(tmpArr[j] > tmpArr[j + 1])
{
int tmp = tmpArr[j];
tmpArr[j] = tmpArr[j + 1];
tmpArr[j + 1] = tmp;
flag = 1;
}
}
if(!flag) break;
}
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr);
}
//快速排序
int Partition(int* arr,unsigned int low,unsigned int high)
{
arr[0] = arr[low];
while(low < high)
{
while(low < high && arr[high] >= arr[0]) --high;
arr[low] = arr[high];
while(low < high && arr[low] <= arr[0]) ++low;
arr[high] = arr[low];
}
arr[low] = arr[0];
return low;
}
void QSort(int* arr,unsigned int low,unsigned int high)
{
if(low < high)
{
int pivotloc = Partition(arr,low,high);
QSort(arr,low,pivotloc - 1);
QSort(arr,pivotloc + 1,high);
}
}
void QuickSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//排序
QSort(tmpArr,1,len);
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr);
}
/*
* 选择排序
*/
//简单选择排序
void SimpleSelectionSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//简单选择排序
for(int i = 1; i <= len; ++i)
{
tmpArr[0] = i;
for(int j = i + 1; j <= len; ++j)
{
if(tmpArr[j] < tmpArr[tmpArr[0]])
{
tmpArr[0] = j;
}
}
if(tmpArr[0] != i)
{
tmpArr[tmpArr[0]] += tmpArr[i];
tmpArr[i] = tmpArr[tmpArr[0]] - tmpArr[i];
tmpArr[tmpArr[0]] = tmpArr[tmpArr[0]] - tmpArr[i];
}
}
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr);
}
//树形选择排序
typedef struct tag_TreeNode
{
int key;
int index;
struct tag_TreeNode* left;
struct tag_TreeNode* right;
struct tag_TreeNode* parent;
}TreeNode;
TreeNode* CreateTreeNode(int key,int index)
{
TreeNode* tn = (TreeNode*)malloc(sizeof(TreeNode));
if(!tn)
{
std::cout << "树形节点内存分配失败!" << std::endl;
return 0;
}
tn->key = key;
tn->index = index;
tn->left = 0;
tn->right = 0;
tn->parent = 0;
}
TreeNode** CreateTreeNodeList(TreeNode** tnList,unsigned int len)
{
int realLen = len / 2;
TreeNode** parentList = (TreeNode**)malloc(sizeof(TreeNode*) * (realLen + (len % 2 != 0)));
for(int i = 0; i < realLen; ++i)
{
int key = INT_MAX;
int index = 0;
if(tnList[2 * i]->key > tnList[2 * i + 1]->key)
{
key = tnList[2 * i + 1]->key;
index = tnList[2 * i + 1]->index;
}
else
{
key = tnList[2 * i]->key;
index = tnList[2 * i]->index;
}
parentList[i] = CreateTreeNode(key,index);
parentList[i]->left = tnList[2 * i];
parentList[i]->right = tnList[2 * i + 1];
tnList[2 * i]->parent = parentList[i];
tnList[2 * i + 1]->parent = parentList[i];
}
if(len % 2 != 0)
{
parentList[realLen] = CreateTreeNode(tnList[2 * realLen]->key,tnList[2 * realLen]->index);
parentList[realLen]->left = tnList[2 * realLen];
parentList[realLen]->right = 0;
tnList[2 * realLen]->parent = parentList[realLen];
}
return parentList;
}
TreeNode* CreateTree(int* arr,unsigned int len,TreeNode**& baseTnList)
{
//初始化基础信息
TreeNode** tnList = (TreeNode**)malloc(sizeof(TreeNode*) * len);
if(!tnList)
{
std::cout << "树形节点列表内存失败!" << std::endl;
return 0;
}
for(int i = 0; i < len; ++i)
{
tnList[i] = CreateTreeNode(arr[i],i);
}
baseTnList = tnList;
//构建上层树节点
while(len > 1)
{
tnList = CreateTreeNodeList(tnList,len);
len = len / 2 + (len % 2 != 0);
}
return tnList ? tnList[0] : 0;
}
void UpdateTree(TreeNode** baseTnList,int index)
{
baseTnList[index]->key = INT_MAX;
TreeNode* parent = baseTnList[index]->parent;
while(parent)
{
TreeNode* left = parent->left;
TreeNode* right = parent->right;
if(left == 0 && right)
{
parent->key = right->key;
parent->index = right->index;
}
else if(right == 0 && left)
{
parent->key = left->key;
parent->index = left->index;
}
else if(left && right)
{
if(left->key < right->key)
{
parent->key = left->key;
parent->index = left->index;
}
else
{
parent->key = right->key;
parent->index = right->index;
}
}
parent = parent->parent;
}
}
void PrintTree(TreeNode* tree)
{
if(!tree) return ;
std::queue<TreeNode*> visitQueue;
visitQueue.push(tree);
while(visitQueue.size() > 0)
{
TreeNode* node = visitQueue.front();
visitQueue.pop();
std::cout << "(" << node->key << "," << node->index << ") ";
if(node->left) visitQueue.push(node->left);
if(node->right) visitQueue.push(node->right);
}
std::cout << std::endl;
}
void ReleaseTree(TreeNode* tree)
{
if(!tree) return ;
ReleaseTree(tree->left);
ReleaseTree(tree->right);
free(tree);
}
void TreeSelectionSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
TreeNode** baseTnList = 0;
TreeNode* tree = CreateTree(arr,len,baseTnList);
//PrintTree(tree);
for(int i = 0; i < len; ++i)
{
std::cout << tree->key << " ";
UpdateTree(baseTnList,tree->index);
}
std::cout << std::endl;
//释放资源
ReleaseTree(tree);
}
//堆调整
void HeapAdjust(int* arr,unsigned int start,unsigned int end)
{
if(!arr || start > end) return ;
arr[0] = arr[start];
for(int j = 2 * start; j <= end; j *= 2)
{
if(j < end && arr[j] < arr[j + 1]) ++j;//选择较大的子节点
if(arr[0] > arr[j]) break;//该子堆已经符合条件
arr[start] = arr[j];
start = j;
}
arr[start] = arr[0];
}
//堆排序
void HeapSelectionSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = (int*)malloc(sizeof(int) * (len + 1));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr + sizeof(int),(char*)arr,sizeof(int) * len);
tmpArr[0] = 0;
//初始化堆
for(int i = len / 2; i > 0; --i)
HeapAdjust(tmpArr,i,len);
//排序
for(int i = len; i > 1; --i)
{
int tmp = tmpArr[1];
tmpArr[1] = tmpArr[i];
tmpArr[i] = tmp;
HeapAdjust(tmpArr,1,i - 1);
}
//输出数组
PrintArray(tmpArr + 1,len);
free(tmpArr);
}
/*
* 归并排序
*/
void Merge(int* sr,int* tr,int s,int m,int e)
{
int k = s;
int j;
for(j = m + 1; s <= m && j <= e; ++k)
{
if(sr[s] <= sr[j]) tr[k] = sr[s++];
else tr[k] = sr[j++];
}
while(s <= m)
{
tr[k++] = sr[s++];
}
while(j <= e)
{
tr[k++] = sr[j++];
}
}
void MSort(int* sr,int* tr,int s,int e,int len)
{
if(s == e) tr[s] = sr[s];
else
{
int* assiArr = (int*)malloc(sizeof(int) * (1 + len));
if(!assiArr)
{
std::cout << "分配辅助内存为空!" << std::endl;
return ;
}
int m = (s + e) / 2;
MSort(sr,assiArr,s,m,len);
MSort(sr,assiArr,m + 1,e,len);
Merge(assiArr,tr,s,m,e);
free(assiArr);
}
}
//2路归并排序(递归)
void TwoWayMergeSortRer(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = 0;
tmpArr = (int*)malloc(sizeof(int) * (1 + len));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr+sizeof(int),(char*)arr,sizeof(int) * len);
//归并排序
MSort(tmpArr,tmpArr,1,len,len);
//输出数组
PrintArray(tmpArr + 1,len);
//资源释放
free(tmpArr);
}
void Merge2(int* sr,int* tr,int s,int m,int e)
{
int k = s;
int j;
int start = s;
int end = e;
for(j = m + 1; s <= m && j <= e; ++k)
{
if(sr[s] <= sr[j]) tr[k] = sr[s++];
else tr[k] = sr[j++];
}
while(s <= m)
{
tr[k++] = sr[s++];
}
while(j <= e)
{
tr[k++] = sr[j++];
}
for(int i = start; i <= end; ++i)
sr[i] = tr[i];
}
void MSort(int* sr,int* tr,int s,int e)
{
if(s < e)
{
int m = (s + e) / 2;
MSort(sr,tr,s,m);
MSort(sr,tr,m + 1,e);
Merge2(sr,tr,s,m,e);
}
}
void TwoWayMergeSortRer2(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = 0;
tmpArr = (int*)malloc(sizeof(int) * (1 + len));
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr+sizeof(int),(char*)arr,sizeof(int) * len);
//辅助数组
int* assiArr = (int*)malloc(sizeof(int) * (1 + len));
if(!assiArr)
{
std::cout << "分配辅助内存为空!" << std::endl;
free(tmpArr);
return;
}
//归并排序
MSort(tmpArr,assiArr,1,len);
//输出数组
PrintArray(tmpArr + 1,len);
//资源释放
free(tmpArr);
free(assiArr);
}
//2路归并排序(非递归)
void TwoWayMergeSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
int* tmpArr = 0;
tmpArr = (int*)malloc(sizeof(int) * len);
if(!tmpArr)
{
std::cout << "分配内存为空!" << std::endl;
return ;
}
memcpy((char*)tmpArr,(char*)arr,sizeof(int) * len);
//归并排序
int mergeTimes = len / 2 + ( len % 2 != 0);//归并次数
int mergeDelta = 1; //归并间隔
for(int i = 1; i <= mergeTimes; i++,mergeDelta *= 2)
{
int mergeGroup = len / ( 2 * mergeDelta);//归并组数
for(int j = 0; j < mergeGroup || (j == 0 && mergeGroup == 0); ++j)
{
int flag = 1;
for(int k = 2 * mergeDelta * j + mergeDelta,c = 1;flag && k < len && c <= mergeDelta; ++c,++k)
{
flag = 0;
if(tmpArr[k] < tmpArr[k - 1])
{
flag = 1;
int tmp = tmpArr[k];
int m = k - 1;
while(m >= 2 * j * mergeDelta && tmp < tmpArr[m])
{
tmpArr[m + 1] = tmpArr[m];
--m;
}
tmpArr[m + 1] = tmp;
}
}
}
}
//输出数组
PrintArray(tmpArr,len);
//资源释放
free(tmpArr);
}
/*
* 基数排序
*/
#define RADIX 10
typedef struct tag_StaticLinkNode
{
int key;
unsigned int next;
}StaticLinkNode;
//获取键值的长度
int GetKeyLen(StaticLinkNode* slnList);
//分配
void Distribute(StaticLinkNode* slnList,int site,unsigned int f[],unsigned int e[]);
//收集
void Collect(StaticLinkNode* slnList,int site,unsigned int f[],unsigned int e[]);
void RadixSort(int* arr,unsigned int len)
{
//初始化
if(!arr)
{
std::cout << "数据源地址为空!" << std::endl;
return ;
}
StaticLinkNode* slnList = (StaticLinkNode*)malloc(sizeof(StaticLinkNode) * (len + 1));
if(!slnList)
{
std::cout << "静态链表内存分配失败!" << std::endl;
return ;
}
slnList[0].key = len;
slnList[0].next = 1;
for(int i = 1; i <= len; ++i)
{
slnList[i].key = arr[i - 1];
slnList[i].next = i + 1;
}
slnList[len].next = 0;
//获取键值的长度
unsigned int f[RADIX];
unsigned int e[RADIX];
int keyLen = GetKeyLen(slnList);
for(int i = 1; i <= keyLen; ++i)
{
Distribute(slnList,i,f,e);
Collect(slnList,i,f,e);
}
//输出结果
for(unsigned int p = slnList[0].next; p ; p = slnList[p].next)
{
std::cout << slnList[p].key << " ";
}
std::cout << std::endl;
//释放资源
free(slnList);
}
//获取关键字长度
int GetKeyLen(int key)
{
int keyLen = (key == 0);
while(key)
{
keyLen += 1;
key /= 10;
}
return keyLen;
}
//获取键值的长度
int GetKeyLen(StaticLinkNode* slnList)
{
int keyLen = 0;
int len = 0;
unsigned int next = slnList[0].next;
while(next)
{
len = GetKeyLen(slnList[next].key);
if(keyLen < len)
{
keyLen = len;
}
next = slnList[next].next;
}
return keyLen;
}
//获取关键字的指定位置的值
int GetKeyWithSite(int key,int site)
{
int num = 0;
if(GetKeyLen(key) >= site)
{
int tmp = pow(10,site - 1);
key /= tmp;
num = key % 10;
}
return num;
}
//分配
void Distribute(StaticLinkNode* slnList,int site,unsigned int f[],unsigned int e[])
{
for(int i = 0; i < RADIX; ++i) f[i] = 0;
for(unsigned int p = slnList[0].next; p ; p = slnList[p].next)
{
unsigned int num = GetKeyWithSite(slnList[p].key,site);
if(!f[num]) f[num] = p;
else slnList[e[num]].next = p;
e[num] = p;
}
}
//收集
void Collect(StaticLinkNode* slnList,int site,unsigned int f[],unsigned int e[])
{
int j = 0;
//找出第一个非空的链表队列
for(j = 0; !f[j]; ++j) ;
slnList[0].next = f[j];
int t = e[j];
while(j < RADIX)
{
//找到下一个非空的链表队列
for(j += 1; j < RADIX - 1 && !f[j]; j += 1);
if(j < RADIX && f[j])
{
slnList[t].next = f[j];
t = e[j];
}
}
slnList[t].next = 0;
}