Heap Sort : Sample program for heap sort

25/08/2009 ·


#include

#include
/* array of MAXARRAY length ... */

#define MAXARRAY 5
void heapsort(int ar[], int len);/* heapsort() to bubble down starting at pos[ition] */

void heapbubble(int pos, int ar[], int len);
int main(void)

{

int array[MAXARRAY];

int i = 0;
/* load some random values into the array */


/* print the original array */

printf("Before heapsort: ");
for(i = 0; i < MAXARRAY;i++)

{

printf(" %d ", array[i]);

}

printf("\n");

heapsort(array, MAXARRAY);
/* print the `heapsorted' array */

printf("After heapsort: ");

for(i = 0; i
{

printf(" %d ", array[i]);

}

printf("\n");

return 0;

}

void heapbubble(int pos, int array[], int len)

{
int z = 0;

int max = 0;

int tmp = 0;

int left = 0;

int right = 0;

z = pos;

for(;;)

{

left = 2 * z + 1;

right = left + 1;

if(left >= len)

return;

else if(right >= len)

max = left;

else if(array[left] > array[right])

max = left;

else

max = right;

if(array[z] > array[max])

return;

tmp = array[z];

array[z] = array[max];

array[max] = tmp;

z = max;

}

}

void heapsort(int array[], int len)

{

int i = 0;

int tmp = 0;

for(i = len / 2; i >= 0; --i)

heapbubble(i, array, len);

for(i = len - 1; i > 0; i--)

{

tmp = array[0];

array[0] = array[i];

array[i] = tmp;

heapbubble(0, array, i);

}

}

0 comments:

Post a Comment

Submit Request

If you need some code or programs just post a comment on this post. I will try to provide you the same at the earliest.

About this blog

Free sample code, example code , code snippet , tutorials in C C++. Find, download and reuse the code database available which vary from small programs to large ones as well. Feel free to request for code that is not in the list.

Followers