首页 > 编程语言 > 详细

常见排序之堆排序

时间:2018-12-08 18:57:21      阅读:197      评论:0      收藏:0      [点我收藏+]

#include<stdio.h>
#include<stdlib.h>

void swap(int *array,int a,int b)
{
int temp;

temp = array[a];
array[a] = array[b];
array[b] = temp;
}


void heap_adjust(int *array,int s,int m)
{
int temp,j;

temp = array[s];

for(j = 2*s; j <=m; j *=2)
{
if(j < m && array[j] < array[j+1])
{
++j;
}

if(temp >= array[j])
{
break;
}

array[s] = array[j];
s = j;
}
array[s] = temp;
}

void heap_sort(int *array,int length)
{
int i,j;

for(i = length/2; i >= 0; i--)
{
heap_adjust(array,i,length);
}

for(j = length-1; j > 0; j--)
{
swap(array,0,j);
heap_adjust(array,0,j-1);
}
}


int main(void)
{
int i;
int array_sort[15] = {14,42,100,50,16,17,3,9,8,60,7,6,4,2,1};

heap_sort(array_sort,15);

for(i = 0; i < 15; i++)
{
printf("%d\t",array_sort[i]);
}
printf("\r\n");

return 0;
}

 

常见排序之堆排序

原文:https://www.cnblogs.com/muzixiaofeng/p/10088625.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!