answersLogoWhite

0

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

16y ago

What else can I help you with?

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).


What is the best case time complexity of heapsort?

The best case time complexity of heapsort is O(n log n), where n is the number of elements in the input array.


What is the worst case time complexity of heapsort?

The worst case time complexity of heapsort is O(n log n), where n is the number of elements in the input array.


Which sorting algorithm is more efficient for large datasets: quicksort or heapsort?

Quicksort is generally more efficient than heapsort for large datasets due to its average time complexity of O(n log n) compared to heapsort's O(n log n) worst-case time complexity.


Is it true that heapsort is empirically just as fast as mergesort?

Empirically, heapsort and mergesort have similar performance in terms of speed, but the specific efficiency may vary depending on the data set and implementation.


What is the best case scenario for heapsort in terms of efficiency and performance?

The best case scenario for heapsort is when the input data is already in a perfect binary heap structure. In this case, the efficiency and performance of heapsort are optimal, with a time complexity of O(n log n) and minimal comparisons and swaps needed to sort the data.


Which sorting algorithm is more efficient for large datasets: heapsort vs quicksort?

Quicksort is generally more efficient than heapsort for large datasets due to its average-case time complexity of O(n log n) compared to heapsort's O(n log n) worst-case time complexity.


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 are the key differences between mergesort and heapsort, and which algorithm is more efficient in terms of time complexity and space complexity?

Mergesort and heapsort are both comparison-based sorting algorithms. The key difference lies in their approach to sorting. Mergesort uses a divide-and-conquer strategy, splitting the array into smaller subarrays, sorting them, and then merging them back together. Heapsort, on the other hand, uses a binary heap data structure to maintain the heap property and sort the elements. In terms of time complexity, both mergesort and heapsort have an average and worst-case time complexity of O(n log n). However, mergesort typically performs better in practice due to its stable time complexity. In terms of space complexity, mergesort has a space complexity of O(n) due to the need for additional space to store the subarrays during the merge phase. Heapsort, on the other hand, has a space complexity of O(1) as it sorts the elements in place. Overall, mergesort is often considered more efficient in terms of time complexity and stability, while heapsort is more space-efficient. The choice between the two algorithms depends on the specific requirements of the sorting task at hand.


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