日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

6-堆排序C实现(递增递减的简单转换,可优化(41行提示))

發布時間:2024/4/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 6-堆排序C实现(递增递减的简单转换,可优化(41行提示)) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、
主函數:void Hea_Sort(int* head,int low,int high,int Step_L,int Bool)
參數解釋
head:數組指針
[low, high]:需排序的數組范圍
Step_L:需排序步長
Bool:等于1表示從小到大排序,不等于1從大到小排序

主函數引用:void Hea_Adjust(int* head,int high,int Start,int Step_L,int Bool)
參數解釋
head:數組指針
Start:需調整的位置
high:數組中能夠進行調整的最高位置
Step_L:需排序步長
Bool:等于1表示從小到大排序,不等于1從大到小排序

說明
Step_L=1時
第一步對下標low+{0,1,2,3,4,5…}排序

Step_L=2時
第一步對下標low+{0,2,4,6,8,10…}排序

Step_L=3時
第一步對下標low+{0,3,6,9,12,15…}排序
以此類推

#include <stdio.h> #include <time.h> void Hea_Sort(int* head,int low,int high,int Step_L,int Bool); void Hea_Adjust(int* head,int high,int Start,int Step_L,int Bool);//堆排序 //需排序數組中第1個元素的位置為low //需排序數組中第i個元素的位置為low+i*Step_L //需排序數組中調整的開始位置為(L-1)/2(邏輯位置),low+(L-1)/2*Step_L(實際地址) void Hea_Sort(int* head,int low,int high,int Step_L,int Bool){//創建根堆int Shigh=low+(((high-low+1)/Step_L)-1+(((high-low+1)%Step_L)!=0))*Step_L;//需排序的最后一個數的下標int L=(Shigh-low)/Step_L+1;//需排序數組長度int Start=low+(L/2-1)*Step_L;//Start為需調整位while(Start>=low){Hea_Adjust(head,high,Start,Step_L,Bool);Start-=Step_L;}printf("\n%d ",head[low]);//輸出大根堆或小根堆的low處的最值//調整根堆head[low]=head[high];//將high處數據放到low處Start=low;//此時,已經是小根堆或大根堆,上一步使得僅有low處數據需要調整,于是將Start置為lowhigh-=Step_L;//需調整的位置while(low<=high){Hea_Adjust(head,high,Start,Step_L,Bool);printf("%d ",head[low]);//輸出大根堆或小根堆的最值head[low]=head[high];//將high處數據放到low處high-=Step_L;//能夠進行調整的最高位置} }//調整函數,為上面堆排序所用 //Bool=1,小堆;Bool=0,大堆 void Hea_Adjust(int* head,int high,int Start,int Step_L,int Bool){int Left=Start+(Start+1)*Step_L;//左子int Right=Start+(Start+2)*Step_L;//右子int temp;//交換while(Left<=high){//左子存在,需要判斷是否調整if(Right<=high){//一父二子比較:Start、Left、Right//簡化可用(((head[Start]-head[Left])*(head[Start]-head[Right])<=0) || (((head[Start]>head[Left])==Bool) && ((head[Start]>head[Right])==Bool)))if(Bool){//小堆,與雙子中最小的交換if(head[Start]>=head[Left] || head[Start]>=head[Right]){//選擇交換子if(head[Left]<=head[Right]){//選擇左交換子temp=head[Start];head[Start]=head[Left];head[Left]=temp;Start=Left;//下一步需調整的位置}else{//選擇右交換子temp=head[Start];head[Start]=head[Right];head[Right]=temp;Start=Right;//下一步需調整的位置}}else{break;}}else{//大堆,與雙子中最大的交換if((head[Start]<=head[Left] || head[Start]<=head[Right])){//選擇交換子if(head[Left]>=head[Right]){//選擇左交換子temp=head[Start];head[Start]=head[Left];head[Left]=temp;Start=Left;//下一步需調整的位置}else{//選擇右交換子temp=head[Start];head[Start]=head[Right];head[Right]=temp;Start=Right;//下一步需調整的位置}}else{break;}}}else{//一父一子比較:Start、Leftif(head[Start]>=head[Left]==Bool){//2021.10.09因為這里的”==”符號用了兩小時尋找!!!temp=head[Start];head[Start]=head[Left];head[Left]=temp;Start=Left;//下一步需調整的位置}else{break;}}Left=Start+(Start+1)*Step_L;//更新左子Right=Start+(Start+2)*Step_L;//更新右子} }int main(int argc, char **argv) {printf("Hello, World!\n");int s[5];s[0]=2;s[1]=5;s[2]=1;s[3]=4;s[4]=3;int i=0;while(i<5)printf("%d ",s[i++]);printf("\n---------\n");Hea_Sort(s,0,4,1,0);return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的6-堆排序C实现(递增递减的简单转换,可优化(41行提示))的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。