Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46100
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-12 17:58
个人简介

熬着熬着,让时间熬成想要的那个自己,一个美丽的人。

文章分类

全部博文(9)

文章存档

2016年(1)

2014年(7)

2013年(1)

分类: C/C++

2014-05-30 17:59:31

c用的比较粗浅,网上观摩了一下都比我的精致,难得能运行成功,先mark一下。

点击(此处)折叠或打开



点击(此处)折叠或打开

  1. #include<stdio.h>
  2. /*void heapAdjust(int num[],int i,int size)
  3. {
  4.     int rchild;
  5.     if(2*i+2>=size)
  6.         rchild==-32768;
  7.     else
  8.         rchild=num[2*i+2];
  9.     int temp=num[i];
  10.     int lchild=num[2*i+1];
  11.     printf(" ---called heapadjust:\n");
  12.     printf(" root:%d,lchild:%d,rchild:%d\n",temp,lchild,rchild);
  13.     if(temp<lchild)
  14.     {
  15.         if(temp<rchild)
  16.         {
  17.             if(lchild<rchild)
  18.             {
  19.                 temp=rchild;
  20.                 num[2*i+2]=num[i];
  21.                 num[i]=temp;
  22.             }
  23.             else
  24.             {
  25.                 num[i]=num[2*i+1];
  26.                 num[2*i+1]=temp;
  27.             }
  28.         }
  29.         else
  30.         {
  31.             num[i]=num[2*i+1];
  32.             num[2*i+1]=temp;
  33.         }
  34.     }
  35.     else
  36.     {
  37.         if(temp<rchild)
  38.         {
  39.             num[i]=num[2*i+2];
  40.             num[2*i+2]=temp;
  41.         }
  42.     }
  43. }*/
  44. void heapAdjust(int num[],int i,int size)
  45. {
  46.     int temp=num[i];
  47.     int lchild=2*i+1;
  48.     while(lchild<size)
  49.     {
  50.         if((lchild<size-1)&&(num[lchild]<num[lchild+1]))
  51.             lchild++;
  52.         if(temp<num[lchild])
  53.         {
  54.             num[i]=num[lchild];
  55.             i=lchild;
  56.             lchild=2*i+1;
  57.         }
  58.         else
  59.             break;

  60.     }
  61.     num[i]=temp;
  62. }
  63. int buildHeap(int num[],int size)
  64. {
  65.     int i;
  66.     for(i=size/2-1;i>=0;i--)
  67.     {
  68.         printf("BUILD HEAP\n");
  69.         heapAdjust(num,i,size);
  70.         printf(" ");
  71.         for(int i=0;i<size;i++)
  72.         {
  73.             printf("%d",num[i]);
  74.             printf(",");
  75.         }
  76.         printf("------Round: %d to build the heap.\n",i);
  77.     }
  78. }
  79. int heapSort(int num[],int size)
  80. {
  81.     int i,temp;
  82.     for(i=size-1;i>=0;i--)
  83.     {
  84.         buildHeap(num,i+1);
  85.         printf("\n");
  86.         printf("---->move the biggest one to later position-----\n");
  87.         printf("%d,%d,%d,--->",i,num[0],num[i]);
  88.         temp=num[0];
  89.         num[0]=num[i];
  90.         num[i]=temp;
  91.         printf("%d,%d--\n",num[0],num[i]);
  92.         printf("now the num[] is:");
  93.         for(int j=0;j<size;j++)
  94.         {
  95.             printf("%d,",num[j]);
  96.             //printf(" \n");
  97.         }
  98.         printf("\n");
  99.         printf("\n");
  100.     }
  101. }
  102. void main()
  103. {
  104.     /*int num[6];
  105.     printf("Enter 6 numbers:\n");
  106.     for(int i=0;i<6;i++)
  107.     {
  108.         scanf("%d",&num[i]);
  109.     }*/
  110.     int num[]={-6,89,47,-99,32762,34,67,-345};
  111.     int size=sizeof(num)/sizeof(int);
  112.     heapSort(num,size);
  113.     printf("-----------------Sorted Array---------------------\n");
  114.     for(int i=0;i<size;i++)
  115.     {
  116.         printf("%d",num[i]);
  117.         printf(",");
  118.     }


  119. }


阅读(856) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~