《编程珠玑》第十一章 排序

好久没写博客了,最近挺忙的,忙的不可开交,可细想一下,又都是在瞎忙,浑浑噩噩的,不知自己到底忙什么,又到底有什么收获。扪心自问,自己浪费了不少时间。不管怎样,自己要把握好,有志者就要时时刻刻约束自己的行为,我要这样严格要求自己,不管别人怎么看待,也不管自己有多痛苦,坚持下去。当然,写博客也是一样,强迫自己每天学点知识,做好总结,同样也做好以后的规划。
好了,废话少说,今天看看了好久未看的《编程珠玑》,是11章,关于排序的,都是很熟悉的东西。下边把我写点的一些关于排序的程序贴上,算术自己的一个小总结;
void Initialization(int *c)
{
//produce a sequence unsorted
for(int i = 0;i < N;i ++)
c[i] = rand()%N;

}
void Display(int *c)
{
//display the current sequence
for(int i = 0;i < N;i ++)
cout<<c[i]<<" ";
cout<<endl;
}
/*Select Sort*/
void SelectSort(int *c,int n)
{
/*find the smaller to the left half in the current right half*/
int tmp;
for(int i=0;i < n-1;i ++)
for(int j=i+1;j < n;j ++)
if (c[j] < c[i])
{
//put the smaller one to left
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
}
/*Insert Sort*/
void InsertSort(int *c,int n)
{
//n numbers
int i,j,tmp;
for(i = 1;i < n;++ i)
{
//sort the c[i]
tmp = c[i];
for(j = i;j > 0&&c[j-1] > tmp;-- j)
//move the bigger ones right
c[j] = c[j-1];
c[j] = tmp;
}
}
void QuickSort(int * c,int l,int u)
{
//quick sort---Version 1
if (l >= u) return;
int m = l;
int tmp;
for (int i = l+1;i <= u;i ++)
if (c[i] < c[l])
{
//swap c[++m] c[i]
tmp = c[++m];
c[m] = c[i];
c[i] = tmp;
}
Display(c);
/*put c[l] in the middle*/
tmp = c[l];
c[l] = c[m];
c[m] = tmp;
/*quick sort the left and right ones*/
QuickSort(c,l,m-1);
QuickSort(c,m+1,u);
}
void _QuickSort(int *c,int l,int u)
{
//improve the above algorithm while all the elements are the same
if (l >= u) return;
int t,i,j,tmp;
t = c[l];
i = l+1;
j = u;
while (true)
{
while(i < u && c[i] <= t) ++ i; cout<<i<<endl;
while (c[j] > t) -- j; cout<<j<<endl;
if (i >= j) break;
/*swap the small and big ones*/
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
Display(c);
}
/*put c[l] in the middle*/
tmp = c[j];
c[j] = c[l];
c[l] = tmp;
/*sort the left and right ones*/
Display(c);
_QuickSort(c,l,j-1);
_QuickSort(c,j+1,u);
}

int get_k_smaller(int *c,int l,int u,int k)
{
//get the k smallest number
//if(l >= u) return;
int t = c[l];
int i,j,tmp;
i = l+1;j=u;
while(true)
{
//cut it to two pieces
while(i < u&&c[i] <= t) ++ i;
while(c[j] > t) -- j;
if (i >= j) break;/*swap all the datas*/
//swap the smaller one and the bigger one
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
/*put the c[l] int the middle*/
tmp = c[l];
c[l] = c[j];
c[j] = tmp;
/*from c[l+1] to c[j] is smaller than t*/
if (j-l+1 == k) /*the same numbers which is k*/
return c[j];
else if (j-l+1 > k) /*the numbers that small than c[l] is more than k*/
get_k_smaller(c,l,j-1,k); /*find the k_smaller in the smaller piece*/
else get_k_smaller(c,j+1,u,k-j+l-1); /*find the last number without the before (j-l+1)s in the smaller one*/
}

/*Shell Sort*/
void ShellSort(int *c,int n)
{
//
int h,tmp;
for(h = 0; h < n;h = 3*h + 1)
;
while (1)
{
h /= 3;
cout<<"h:"<<h<<endl;
if (h < 1) break;
for (int i = h;i < n;++ i)
for(int j = i;j >= h;j -= h)
{
if (c[j-h] < c[j]) break;
//swap
tmp = c[j-h];
c[j-h] = c[j];
c[j] = tmp;
cout<<i<<" "<<j<<endl;
Display(c);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值