12.21

快速排序

int Partition ( SqList L,int low, int high ){
L.elem[0]=L.elem[low];
int pivotkey=L.elem[0];
while(low<high){
while(low<high&&L.elem[high]>=pivotkey) high--;
L.elem[low]=L.elem[high];
while(low<high&&L.elem[low]<=pivotkey) low++;
L.elem[high]=L.elem[low];
}
L.elem[low]=L.elem[0];
return low;
}

 堆排序

void HeapAdjust( HeapType H, int s, int m){
//假设r[s+1..m]已经是堆,将r[s..m]调整为以r[s]为根的大根堆
KeyType rc;
int j;
rc=H.elem[s];
for(j=2*s;j<=m;j*=2)
{//沿key较大的孩子结点向下筛选
if(j<m&&H.elem[j]<H.elem[j+1]) ++j;//j为key较大的记录的下标
if(rc>=H.elem[j]) break; //rc应插入在位置s上
H.elem[s]=H.elem[j]; s=j;
}
H.elem[s]=rc;//插入
}

 

posted @   晨观夕  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示