answersLogoWhite

0

#include <stdlib.h>

#include <stdio.h>

#define uint unsigned int

typedef int (*compare_func)(int, int);

void heap_sort(int This[], compare_func func_pointer, uint len)

{

/* heap sort */

uint half;

uint parents;

if (len <= 1)

return;

half = len >> 1;

for (parents = half; parents >= 1; --parents)

{

int tmp;

int level = 0;

uint child;

child = parents;

/* bottom-up downheap */

/* leaf-search for largest child path */

while (child <= half)

{

++level;

child += child;

if ((child < len) &&

((*func_pointer)(This[child], This[child - 1]) > 0))

++child;

}

/* bottom-up-search for rotation point */

tmp = This[parents - 1];

for (;;)

{

if (parents child)

break;

if ((*func_pointer)(tmp, This[child - 1]) <= 0)

break;

child >>= 1;

--level;

}

/* rotate nodes from parents to rotation point */

for (;level > 0; --level)

{

This[(child >> level) - 1] =

This[(child >> (level - 1)) - 1];

}

This[child - 1] = tmp;

} while (--len >= 1);

}

#define ARRAY_SIZE 250000

int my_array[ARRAY_SIZE];

void init()

{

int indx;

for (indx=0; indx < ARRAY_SIZE; ++indx)

{

my_array[indx] = rand();

}

}

int cmpfun(int a, int b)

{

if (a > b)

return 1;

else if (a < b)

return -1;

else

return 0;

}

int main()

{

int indx;

init();

heap_sort(my_array, cmpfun, ARRAY_SIZE);

for (indx=1; indx < ARRAY_SIZE; ++indx)

{

if (my_array[indx - 1] > my_array[indx])

{

printf("bad sort\n");

return(1);

}

}

return(0);

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Write a program of binary heap in c or c language?

to implement operations on binary heap in c


What is the C program for heap sort using recursion?

123


Different types of sorting techniques in c language?

types of sorting in c language are: insertion sort selection sort bubble sort merge sort two way merge sort heap sort quick sort


Different parts of c language program?

what are the parts of C language program


Using doublelinked list insertion sort in c language?

using doublelinked list insertion sort in c language


What has the author David Spuler written?

David Spuler has written: 'Comprehensive C' -- subject(s): C (Computer program language) 'C++ and C efficiency' -- subject(s): C++ (Computer program language), C (Computer program language)


Program for sin series in c language?

find the program in c-pgms.blogspot.com


C program was introduced in the year?

c program was introduced in the year 1972 by Dennis RitchieNo, it was the C language, not the C program.


What has the author Conor Sexton written?

Conor Sexton has written: 'Newnes C++ pocket book' -- subject(s): C (Computer program language), C++ (Computer program language) 'C pocket book' -- subject(s): C (Computer program language) 'Y2K7'


What do you mean by c taken programming in c?

A C program is a computer program written using the C programming language.


What has the author Steve Schustack written?

Steve Schustack has written: 'C for fun and profit' -- subject(s): C (Computer program language) 'Variations in C' -- subject(s): C (Computer program language) 'C for fun and profit' -- subject(s): C (Computer program language) 'Variations In C Edition'


How there is security in c language?

The c language is as secure or as insecure as the programmer makes it. Security is not in the language; it is in the design of the program.