一个苹果,n种思想~


关于堆排序

Posted in C/C++ by hongliang on the 六月 18th, 2007

tree.jpg数据结构基础的确很烂,想想还是多写写代码会好些,堆排序是选择排序的深化,是一种树型结构的排序.(老鸟不要笑我阿,小弟学习中)代码如下,gcc编译通过.

下载: HeapSort.c
  1. #include"stdio.h"
  2.  
  3. #define MAX_SIZE 100
  4.  
  5. void HeapAdjust(int R[],int s,int m);
  6. void HeapSort(int R[],int n);
  7.  
  8. void HeapAdjust(int R[],int s,int m)
  9. {
  10. int j;
  11. R[0] = R[s];
  12. for(j = 2 * s; j <= m ; j *= 2)
  13. {
  14. if(j < m && R[j] < R[j+1])
  15. j++;/*j为左右子树中较大的下标*/
  16. if(R[0] >= R[j])
  17. break;/*j<=m-1和temp>=的排序的元素,均为结束条件*/
  18. R[s] = R[j];
  19. s = j;
  20. }
  21. R[s] = R[0];
  22. }
  23.  
  24. void HeapSort(int R[],int n)
  25. {
  26. int i;
  27. for(i = n/2; i > 0; i--)
  28. HeapAdjust(R,i,n);/*建立大顶堆*/
  29. for(i = n; i > 1; i--)
  30. {
  31. R[0] = R[1];
  32. R[1] = R[i];
  33. R[i] = R[0];/*交换第一个和最后一个元素*/
  34. HeapAdjust(R,1,i-1);
  35. }
  36. }
  37.  
  38. int main(void)
  39. {
  40. int i,a[MAX_SIZE],n;
  41. printf("请输入要排序的个数N\n");
  42. scanf("%d",&n);
  43. printf("请输入排序的数字(N个)\n");
  44. for(i = 1; i <=n ; i++)
  45. scanf("%d",&a[i]);
  46. HeapSort(a,n);
  47. printf("利用堆排序的结果为:\n");
  48. for( i = 1; i <= n; i++)
  49. printf("%3d",a[i]);
  50. printf("\n");
  51. return 0;
  52. }

阅读(1239 次)

生活

Posted in 生活 by hongliang on the 六月 14th, 2007

basketball一天总在电脑面前不免有些反应迟缓,今天晚上去打篮球,感觉好多了,去外面感受一下阳光与篮球,是多么快乐的一件事情:)

阅读(773 次)

pku acm题目分类 (4)

Posted in C/C++ by hongliang on the 六月 12th, 2007

按照ac的代码长度分类
短代码:0.01K–0.50K;中短代码:0.51K–1.00K;中等代码量:1.01K–2.00K;长代码:2.01K以上。 (more…)

阅读(1132 次)

pku acm题目分类 (3)

Posted in C/C++ by hongliang on the 六月 12th, 2007

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)

Posted in C/C++ by hongliang on the 六月 12th, 2007

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 次)

« Previous PageNext Page »

Close
E-mail It