life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
#include<iostream.h>
#define N 13

void sift(int* a,int low,int high){
    
int i=low;
    
int j=2*i;
    
int temp=a[i];
    
while(j<=high){
          
if (j<high && a[j]<a[j+1])
          
{
              j
++;
          }

          
if (temp<a[j])
          
{
              a[i]
=a[j];
              i
=j;
              j
=2*i;
          }
else
              
break;

    }

    a[i]
=temp;
}


void heap_sort(int* a,int n){
   
int i;
   
int temp;
   
for (i=n/2;i>=0;i--)
   
{
       sift(a,i,n
-1);
   }

   
for (i=n-1;i>0;i--)
   
{
       temp
=a[0];
       a[
0]=a[i];
       a[i]
=temp;
       sift(a,
0,i-1);
   }


}


void sort_print(int* a,int n){
  
for (int i=0;i<n;i++)
  
{
      cout
<<a[i]<<" ";
  }

  cout
<<endl;

}


int main(){

    
int a[N]={34,34,566,66,77,8,989,6676,12323,89,90,123,4355};
    sort_print(a,N);
    heap_sort(a,N);
    sort_print(a,N);



}
posted on 2009-09-28 22:28 life02 阅读(216) 评论(0)  编辑 收藏 引用 所属分类: 算法

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理