answersLogoWhite

0


Best Answer

Heapsort(A) {

BuildHeap(A)

for i <- length(A) downto 2 {

exchange A[1] <-> A[i]

heapsize <- heapsize -1

Heapify(A, 1)

}

BuildHeap(A) {

heapsize <- length(A)

for i <- floor( length/2 ) downto 1

Heapify(A, i)

}

Heapify(A, i) {

le <- left(i)

ri <- right(i)

if (le<=heapsize) and (A[le]>A[i])

largest <- le

else

largest <- i

if (ri<=heapsize) and (A[ri]>A[largest])

largest <- ri

if (largest != i) {

exchange A[i] <-> A[largest]

Heapify(A, largest)

}

}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a c-code to implement heapsort?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the worst case and best case time complexity of heapsort?

The best and worst case time complexity for heapsort is O(n log n).


How do you get a book on club penguincom?

Umm you could just type in the books anme and they'll give eyou the ccode


What is the average efficiency of heapsort?

The efficiency class is O (n log n).See the related link for further information.


Write a c program to implement heap sort?

#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* array of MAXARRAY length ... */ #define MAXARRAY 5 /* preform the heapsort */ void heapsort(int ar[], int len); /* help 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 */ for(i = 0; i &lt; MAXARRAY; i++) array[i] = rand() % 100; /* print the original array */ printf("Before heapsort: "); for(i = 0; i &lt; MAXARRAY; i++) { printf(" %d ", array[i]); } printf("\n"); heapsort(array, MAXARRAY); /* print the `heapsorted' array */ printf("After heapsort: "); for(i = 0; i &lt; MAXARRAY; 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 &gt;= len) return; else if(right &gt;= len) max = left; else if(array[left] &gt; array[right]) max = left; else max = right; if(array[z] &gt; 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 &gt;= 0; --i) heapbubble(i, array, len); for(i = len - 1; i &gt; 0; i--) { tmp = array[0]; array[0] = array[i]; array[i] = tmp; heapbubble(0, array, i); } }


What is the running time of heapsort on an array A of length n that is already sorted in increasing order?

The running time of HEAPSORT on an array A of length n that is already sorted in increasing order is (n lg n) because even though it is already sorted, it will be transformed back into a heap andsorted.The running time of HEAPSORT on an array A of length n that is sorted in decreasing order willbe (n lg n). This occurs because even though the heap will be built in linear time, every time themax element is removed and the HEAPIFY is called it will cover the full height of the tree


What should be done immediately be done after you assess the hazards to determine the risk?

Implement controls


Can you put implement in a complete sentence?

implement - noun - what kind of implement do you use for weeding around the plants? implement - verb - We will implement the new policies as soon as possible.


Can you Give me a sentence for the word implement?

An implement is a tool. A hoe is an implement; so is a shovel.


Does a horse count as a implement?

No an implement is some kind of tool.


When you implement are you avoiding it?

No. To implement is to start or put something into place


Is re-implement or reimplement the correct spelling?

re-implement


What is a sentence for implement that includes the definition?

I intend to use that dictionary to find the definition of 'implement'. The shovel was wielded as an implement of destruction by the defendant.