answersLogoWhite

0

#include <stdio.h>

#include <stdlib.h>

/* 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 < MAXARRAY; i++)

array[i] = rand() % 100;

/* 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 < 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 >= 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);

}

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What is the C program for heap sort using recursion?

123


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


What is the best data structure for heap sort?

selection sort


How do you write a standard basic program?

That really depends on what sort of program you are trying to build, what do you want the program to do?


Where can you find source code of heap sort in OpenGL?

Heap sort is not directly related to OpenGL, as it is a sorting algorithm rather than a graphics API. However, you can find implementations of heap sort in various programming languages on platforms like GitHub or in educational resources about algorithms. If you're looking for sorting within an OpenGL context (e.g., sorting vertices or pixels), you may find relevant code snippets or examples in OpenGL tutorials or graphics programming books that implement sorting algorithms.


What is the worst case scenario for the Heap Sort algorithm in terms of time complexity and how does it compare to other sorting algorithms?

The worst case scenario for the Heap Sort algorithm is O(n log n) time complexity, which means it can be slower than other sorting algorithms like Quick Sort or Merge Sort in certain situations. This is because Heap Sort requires more comparisons and swaps to rearrange the elements in the heap structure.


How do you sort the given contents of an array?

You would sort the given elements of an array by a bubble sort or heap sort code!!


What is the running time of heap sort algorithm?

The running time of the heap sort algorithm is O(n log n), where n is the number of elements in the input array.


What is the runtime complexity of the heap sort algorithm?

The runtime complexity of the heap sort algorithm is O(n log n), where n is the number of elements in the input array.


What is the time complexity of heap sort algorithm?

The time complexity of the heap sort algorithm is O(n log n), where n is the number of elements in the input array.


What are the differences between heap sort and merge sort, and which one is more efficient in terms of time complexity?

Heap sort and merge sort are both comparison-based sorting algorithms. The main difference between them is in their approach to sorting. Heap sort uses a binary heap data structure to sort elements. It repeatedly extracts the maximum element from the heap and places it at the end of the sorted array. This process continues until all elements are sorted. Merge sort, on the other hand, divides the array into two halves, sorts each half recursively, and then merges the sorted halves back together. In terms of time complexity, both heap sort and merge sort have a time complexity of O(n log n) in the worst-case scenario. However, in practice, merge sort is often considered more efficient because it has a more consistent performance across different input data sets. Heap sort can have a higher constant factor in its time complexity due to the overhead of maintaining the heap structure.


What is the best case scenario for the performance of heap sort algorithm?

The best case scenario for the performance of the heap sort algorithm is when the input data is already in a perfect heap structure, resulting in a time complexity of O(n log n).