//排序
#include
#include
#include
#define MAXSIZE 99
typedef struct{
int data[MAXSIZE+1];//0位哨兵位从1开始排序
int length;
}*SqList,sqlist;
//插入排序
void ChaRuPaiXv(SqList L){
for(int i=2;i<=L->length;i++){
L->data[0]=L->data[i];
int j;
for(j=i-1;j>=0&&L->data[0]>L->data[j];j--)
L->data[j+1]=L->data[j];
L->data[j+1]=L->data[0];
}
}
//折半插入排序
void ZheBanPaiXv(SqList L){
for(int i=2;ilength;i++){
L->data[0]=L->data[i];
int low=1,high=i-1,mid;
while(L->data[low]<=L->data[high]){
mid=(low+high)/2;
if(L->data[i]>L->data[mid]){
low=mid+1;
}else high=mid-1;
}//循环结束,high+1为插入位置
for(int j=i-1;j>=high+1;j--){
L->data[j+1]=L->data[j];
}
L->data[high+1]=L->data[0];
}
}
/*希尔排序 就是间隔为互质序列Dk的项为一组进行插入排序
*变成ABCABCABC A为一组排序BC各一组,再把间隔缩小再排
*直至间隔为1*/
void ChaRuJiannGeK(SqList L,int K);//间隔为K的插入排序
void XiErPaiXv(SqList L,int D[],int t){//D[]序列 t趟数
for(int i=0;ilength;i++){
L->data[0]=L->data[i];
int j;
for(j=i-K;j>0&&(L->data[0]>L->data[j]);j=j-K){
L->data[j+K]=L->data[j];
}
L->data[j+K]=L->data[0];
}
}
//冒泡排序 简单 略
//快速排序
int Partition(SqList,int,int);
void KuaiSuPaiXv(SqList L,int low,int high){
if(lowdata[0]=L->data[low];
int pivot=L->data[low];
while(lowdata[high]>=pivot)){
--high;
}
L->data[low]=L->data[high];
while(lowdata[low]<=pivot)){
++low;
}
L->data[high]=L->data[low];
}
L->data[low]=L->data[0];
return low;
}
//选择排序 选出每次最小(大)的换到第i个
void XuanZePaiXv(SqList L){
for(int i=1;ilength;i++){
int t,j;
for(j=i+1;jlength;j++){
if(L->data[j]>L->data[i]){
t=L->data[i];
L->data[i]=L->data[j];
L->data[j]=t;
}
}
}
}
//堆排序
void HeapAdjust(SqList L,int k,int end);/*整理堆子函数 k为这个根结点序号
*end为这棵树最后结点序号*/
void DuiPaiXv(SqList L){
int k,end=(L->length-1);
for(k=end/2;k>=1;k--){/*从完全二叉树的最后一层向上整理*/
HeapAdjust(L,k,end);//整理堆
}
for(k=1;klength-1;k++){
L->data[0] =L->data[end-(k-1)];/*队尾放有序列*/
L->data[end-(k-1)]=L->data[1];
L->data[1]= L->data[0];/*第一个end第二个end-1*/
HeapAdjust(L,1,end-k);
}
}
void HeapAdjust(SqList L,int k,int end){//大根堆
int i=k,j=2*i;
while(j<=end){
if(jdata[j]data[j+1])/*j=end则其无右子树*/
j=j+1; /*jdata[j]>L->data[i]){
L->data[0]=L->data[i];
L->data[i]=L->data[j];
L->data[j]=L->data[0];/*弱j更大则交换i*/
}
i=j;j=2*i;/*继续判断子树*/
}
}
//归并排序
//由小到大结合函数
int T[MAXSIZE];//辅助数组T
void Merge (int* R,int* T,int low,int mid,int high){
int i=low,j=mid+1,k=low;/*R[i] R[j] 大到小合并到T[k]*/
while(i<=mid&&j<=high){
if(R[i]>R[j]){
T[k++]=R[i++];
} else{
T[k++]=R[j++];
}/*选大的放入T中*/
}
while(i<=mid){
T[k++]=R[i++];/*当j空了把剩余i接上*/
}
while(j<=high){
T[k++]=R[j++];/*i空接j*/
}
for(i=low;i<=high;i++){/*写回R[],不写回之后将是排序前的数组比较*/
R[i]=T[i];
}
}
//递归拆分函数
void Sort(int*R,int*T,int low,int high){
if(low>=high) return;
else{
int mid=(low+high)/2;
Sort(R,T,low,mid);
Sort(R,T,mid+1,high);
Merge(R,T,low,mid,high);
}
}
//桶排序 两位数排
void TongPaiXv(SqList L){
/*按位数排准备十个桶最大也就十个*/
int a0[10]={0},a1[10]={0},a2[10]={0},a3[10]={0},a4[10]={0},a5[10]={0},a6[10]={0},a7[10]={0},a8[10]={0},a9[10]={0};
int b[10]={0};//各位表示桶内插入位置
/*按个位扔桶里*/
for(int i=1;ilength;i++){
int a=L->data[i]%10;
switch (a) {
case 0:
a0[(b[0]++)]=L->data[i];
break;
case 1:
a1[(b[1]++)]=L->data[i];
break;
case 2:
a2[(b[2]++)]=L->data[i];
break;
case 3:
a3[(b[3]++)]=L->data[i];
break;
case 4:
a4[(b[4]++)]=L->data[i];
break;
case 5:
a5[(b[5]++)]=L->data[i];
break;
case 6:
a6[(b[6]++)]=L->data[i];
break;
case 7:
a7[(b[7]++)]=L->data[i];
break;
case 8:
a8[(b[8]++)]=L->data[i];
break;
case 9:
a9[(b[9]++)]=L->data[i];
break;
}
}
/*然后把他们从桶里倒出来*/
int pos=1;/*L->data的位置*/
for(int i=0;i<=9;i++){
if(b[i]!=0){
for(int j=0;jdata[pos++]=a0[j];
break;
case 1:
L->data[pos++]=a1[j];
break;
case 2:
L->data[pos++]=a2[j];
break;
case 3:
L->data[pos++]=a3[j];
break;
case 4:
L->data[pos++]=a4[j];
break;
case 5:
L->data[pos++]=a5[j];
break;
case 6:
L->data[pos++]=a6[j];
break;
case 7:
L->data[pos++]=a7[j];
break;
case 8:
L->data[pos++]=a8[j];
break;
case 9:
L->data[pos++]=a9[j];
break;
}
}
}
}
/*该十位了 上面步骤再来一次*/
//先把桶清空
memset(a0,0,40);memset(a1,0,40);memset(a2,0,40);memset(a3,0,40);memset(a4,0,40);
memset(a5,0,40);memset(a6,0,40);memset(a7,0,40);memset(a8,0,40);memset(a9,0,40);
memset(b,0,40);
for(int i=1;ilength;i++){
int a=L->data[i]/10;
switch (a) {
case 0:
a0[(b[0]++)]=L->data[i];
break;
case 1:
a1[(b[1]++)]=L->data[i];
break;
case 2:
a2[(b[2]++)]=L->data[i];
break;
case 3:
a3[(b[3]++)]=L->data[i];
break;
case 4:
a4[(b[4]++)]=L->data[i];
break;
case 5:
a5[(b[5]++)]=L->data[i];
break;
case 6:
a6[(b[6]++)]=L->data[i];
break;
case 7:
a7[(b[7]++)]=L->data[i];
break;
case 8:
a8[(b[8]++)]=L->data[i];
break;
case 9:
a9[(b[9]++)]=L->data[i];
break;
}
}
/*然后把他们从桶里倒出来*/
pos=1;/*L->data的位置*/
for(int i=0;i<=9;i++){
if(b[i]!=0){
for(int j=0;jdata[pos++]=a0[j];
break;
case 1:
L->data[pos++]=a1[j];
break;
case 2:
L->data[pos++]=a2[j];
break;
case 3:
L->data[pos++]=a3[j];
break;
case 4:
L->data[pos++]=a4[j];
break;
case 5:
L->data[pos++]=a5[j];
break;
case 6:
L->data[pos++]=a6[j];
break;
case 7:
L->data[pos++]=a7[j];
break;
case 8:
L->data[pos++]=a8[j];
break;
case 9:
L->data[pos++]=a9[j];
break;
}
}
}
}
}
int main(){
SqList L;
L=(SqList)malloc(MAXSIZE*sizeof(sqlist));
L->length=0;
for(int i=0;i<=15;i++){
L->data[i]=i;
L->length++;
}
ChaRuPaiXv(L);
ZheBanPaiXv(L);
int D[3]={5,3,1};
XiErPaiXv(L,D,3);
KuaiSuPaiXv(L,1,L->length-1);
XuanZePaiXv(L);
DuiPaiXv(L);
Sort(L->data,T,1,L->length);/*归并排序T辅助数组*/
TongPaiXv(L);
for(int i=0;ilength;i++){
printf("%d ",L->data[i]);
}
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)