关于堆排序
数据结构基础的确很烂,想想还是多写写代码会好些,堆排序是选择排序的深化,是一种树型结构的排序.(老鸟不要笑我阿,小弟学习中)代码如下,gcc编译通过.
- #include"stdio.h"
- #define MAX_SIZE 100
- void HeapAdjust(int R[],int s,int m);
- void HeapSort(int R[],int n);
- void HeapAdjust(int R[],int s,int m)
- {
- int j;
- R[0] = R[s];
- for(j = 2 * s; j <= m ; j *= 2)
- {
- if(j < m && R[j] < R[j+1])
- j++;/*j为左右子树中较大的下标*/
- if(R[0] >= R[j])
- break;/*j<=m-1和temp>=的排序的元素,均为结束条件*/
- R[s] = R[j];
- s = j;
- }
- R[s] = R[0];
- }
- void HeapSort(int R[],int n)
- {
- int i;
- for(i = n/2; i > 0; i--)
- HeapAdjust(R,i,n);/*建立大顶堆*/
- for(i = n; i > 1; i--)
- {
- R[0] = R[1];
- R[1] = R[i];
- R[i] = R[0];/*交换第一个和最后一个元素*/
- HeapAdjust(R,1,i-1);
- }
- }
- int main(void)
- {
- int i,a[MAX_SIZE],n;
- printf("请输入要排序的个数N\n");
- scanf("%d",&n);
- printf("请输入排序的数字(N个)\n");
- for(i = 1; i <=n ; i++)
- scanf("%d",&a[i]);
- HeapSort(a,n);
- printf("利用堆排序的结果为:\n");
- for( i = 1; i <= n; i++)
- printf("%3d",a[i]);
- printf("\n");
- return 0;
- }
阅读(1239 次)
pku acm题目分类 (4)
按照ac的代码长度分类
短代码:0.01K–0.50K;中短代码:0.51K–1.00K;中等代码量:1.01K–2.00K;长代码:2.01K以上。 (more…)
阅读(1132 次)
pku acm题目分类 (3)
1770 Special Experiment树形DP 81% 2005-3-24
1771 Elevator Stopping Plan DP 73% 2005-4-27
1772 New Go Game构造? 91% 2005-3-10
1773 Outernet模拟 62% 2005-4-16 (more…)
阅读(2534 次)
pku acm题目分类 (2)
1500 Polygonal Puzzle 86% 2005-4-26
1501 Word-Search Wonder 55% 2005-5-1
1502 MPI Maelstrom 42% 2005-4-9
1503 Integer Inquiry 67% 2005-5-7 (more…)
阅读(1007 次)
pku acm题目分类 (1)
多版本pku题目分类及算法分类
POJ各题算法
1000 A+B Problem 送分题 49% 2005-5-7
1001 Exponentiation 高精度 85% 2005-5-7
1002 487-3279 n/a 90% 2005-5-7
1003 Hangover 送分题 62% 2005-5-7 (more…)
阅读(2543 次)