生活随笔
收集整理的這篇文章主要介紹了
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
);
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
;while(Start
>=low
){Hea_Adjust(head
,high
,Start
,Step_L
,Bool
);Start
-=Step_L
;}printf("\n%d ",head
[low
]);head
[low
]=head
[high
];Start
=low
;high
-=Step_L
;while(low
<=high
){Hea_Adjust(head
,high
,Start
,Step_L
,Bool
);printf("%d ",head
[low
]);head
[low
]=head
[high
];high
-=Step_L
;}
}
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
){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{if(head
[Start
]>=head
[Left
]==Bool
){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行提示))的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。