Heap Sort is one of the best sorting
methods being in-place and with no quadratic worst-case scenarios. The algorithm can be divided into 2 basic parts:
[1] Creating a Heap of the unsorted list. [2] Then a sorted array is created by repeatedly removing the largest/smallest element from the heap, and inserting it into the array. The heap is reconstructed after each removal.
[1] Creating a Heap of the unsorted list. [2] Then a sorted array is created by repeatedly removing the largest/smallest element from the heap, and inserting it into the array. The heap is reconstructed after each removal.
Heap
is
a tree-based data structure that satisfies two special properties:
*** Heap data structure is always a Complete Binary Tree, which means all levels of the tree are fully filled.
How
Heap Sort Works: Initially
on receiving an unsorted list, the first step is to create a Heap
data structure (Max-Heap or Min-Heap). Once heap is built, the first element of
the Heap is either largest or smallest (based on Max-Heap or Min-Heap),
now we can put the first element of the heap in the array. Then we again make heap
using the remaining elements, to again pick the first element of the heap and
put it into the array. We keep on doing the same repeatedly until we have the
complete sorted list in the array.
*** Heap data structure is always a Complete Binary Tree, which means all levels of the tree are fully filled.
*** All nodes are either [greater than or equal to]
or [less than or equal to] each of its children. If the parent nodes are
greater than their children, heap is called a Max-Heap, and if the parent nodes
are smaller than their child nodes, heap is called Min-Heap.
Let { 80, 32, 31, 110, 50, 40, 120 } be the list that we want to sort from the smallest to the largest (Max-heap).
Now all elements are sorted: 31, 32, 40, 50, 80, 110, 120.
Source
Code - Method 01:
#include <stdio.h>
#include <stdlib.h>
#define ValType double
#define IS_LESS(v1, v2) (v1 < v2)
// To sort in descending order, change
< to > only.
#define SWAP(r,s) do{ValType t=r; r=s; s=t; } while(0)
void siftDown( ValType *a, int start,
int count);
void heapsort( ValType *a, int count)
{
int start, end;
for (start = (count-2)/2; start
>=0; start--) {
siftDown( a, start, count);
}
for (end=count-1; end > 0; end--) {
SWAP(a[end],a[0]);
siftDown(a, 0, end);
}}
void siftDown( ValType *a, int start,
int end) {
int root = start;
while ( root*2+1 < end ) {
int child = 2*root + 1;
if ((child + 1 < end) &&
IS_LESS(a[child],a[child+1])) {
child += 1;
}
if (IS_LESS(a[root], a[child])) {
SWAP( a[child], a[root] );
root = child;
}
else
return;
}}
int main() {
int n;
double valsToSort[] = {1.3, 50.9,
15.22, -1.77, 301.501, 0.3301, 40.17, 30.54, -37.2, 49.6};
#define VSIZE (sizeof(valsToSort)/sizeof(valsToSort[0]))
heapsort(valsToSort, VSIZE);
for (n=0; n<VSIZE; n++)
printf("( %.3f )\n\n", valsToSort[n]);
return 0;
}
Source Code - Method 02:
#include<stdio.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
main() {
int n,i,a[50];
printf("\nEnter How Many Number
You Want To Sort : ");
scanf("%d",&n);
printf("\nEnter %d Numbers:
\n", n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\nThe Sorted Numbers Are:
");
for (i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");
}
void heapsort(int a[],int n) {
int i,t;
heapify(a,n);
for (i=n-1;i>0;i--) {
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a,i);
}}
void heapify(int a[],int n) {
int k,i,j,item;
for (k=1;k<n;k++)
{
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j])) // To sort in descending order, change item
> a[j] to item < a[j]
{
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}}
void adjust(int a[],int n) {
int i,j,item;
j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1)
{
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i])
{
a[j] = a[i];
j = i;
i = 2*j+1;
}
else break;
}
a[j] = item;
}
Source Code - Method 03:
#include <stdio.h>
#include <stdlib.h>
struct MaxHeap {
int size;
int* array;
};
void swap(int* a, int* b) { int t =
*a; *a = *b; *b = t; }
void maxHeapify(struct MaxHeap*
maxHeap, int idx)
{
int largest = idx;
int left = (idx << 1) + 1;
int right = (idx + 1) << 1;
if (left < maxHeap->size
&& maxHeap->array[left]
> maxHeap->array[largest]) largest = left;
//for arranging numbers in descending
order, change to maxHeap-> array[left] < maxHeap->array[largest]
if (right < maxHeap->size
&& maxHeap->array[right]
> maxHeap->array[largest]) largest = right;
//for arranging numbers in descending
order, change to maxHeap-> array[right] < maxHeap->array[largest]
if (largest != idx) {
swap(&maxHeap->array[largest],
&maxHeap->array[idx]);
maxHeapify(maxHeap, largest);
}}
struct MaxHeap* createAndBuildHeap(int
*array, int size)
{
int i;
struct MaxHeap* maxHeap = (struct
MaxHeap*) malloc(sizeof(struct MaxHeap));
maxHeap->size = size;
maxHeap->array = array;
for (i = (maxHeap->size - 2) / 2; i
>= 0; --i)
maxHeapify(maxHeap, i);
return maxHeap;
}
void heapSort(int* array, int size) {
struct MaxHeap* maxHeap = createAndBuildHeap(array,
size);
while (maxHeap->size > 1) {
swap(&maxHeap->array[0],
&maxHeap->array[maxHeap->size - 1]);
--maxHeap->size;
maxHeapify(maxHeap, 0);
}}
void printArray(int* arr, int size) {
int i;
for (i = 0; i < size; ++i)
printf("%d ", arr[i]);
}
int main() {
int arr[] = {74,12,15,10,5,17,9,35,6};
int size = sizeof(arr)/sizeof(arr[0]);
printf(" The Unsorted Numbers
Are: ");
printArray(arr, size);
heapSort(arr, size);
printf("\n\n After Sorting Them
In Ascending Order: ");
printArray(arr, size);
printf("\n");
return 0;
}