facebook like button

22 November, 2011

Heap sort in C

The need:
   Heap sort is a better than bubble, selection and insertion algorithm for sorting. To show how heap can be useful in real life implementation.
The code:
-----------------------------------------------------------
/*this program is an array implementation of heap*/ 
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 

void print_array(int a[],int array_size); 
void heap_sort(int a[],int array_size); 
void heapify(int a[],int count); 
void adjust_down(int a[],int start, int end); 

main() 
{ 
    int i,arr[50],size=0; 
    arr[0]=0; 
    printf("how many elements do you want to enter\n");
    scanf("%d",&size); 
    if(size<1)
    {
        printf("You did not enter a valid count of elements.\n");
        printf("Rerun the program and try again\n");
        exit(0);
    }
    printf("enter array\n"); 
    for(i=1;i<=size;i++) 
        scanf("%d",&arr[i]);
    printf("the array before heap sort.\n"); 
    print_array(arr,size); 
    heap_sort(arr,size);
    printf("the array after heap sort.\n"); 
    print_array(arr,size);    
} 

void heap_sort(int a[],int array_size)
{
    int i,j,heap_end,temp;
    heap_end=array_size;
    heapify(a,array_size);
    while(heap_end>2)
    {
        temp=a[1];
        a[1]=a[heap_end];
        a[heap_end]=temp;
        heap_end--;
        adjust_down(a, 1, heap_end);
    }
}

void heapify(int a[],int count) 
{ 
    int start;
    start = count/2 + 1;
    while(start>0)
    {
        adjust_down(a, start, count);
        start--;
    } 
} 

void adjust_down(int a[],int start, int end)
{
    int root = start,temp,bigger_child;
    while(2*root <= end)
    {
        if(2*root+1 > end)
        {
            if(a[2*root]<a[root])
            {
                temp=a[root];
                a[root]=a[2*root];
                a[2*root]=temp;
            }
            root=2*root;
        }
        else
        {
            bigger_child = (a[2*root]>a[2*root+1])? 2*root : 2*root+1;
            if(a[root]<a[bigger_child])
            {
                temp=a[root];
                a[root]=a[bigger_child];
                a[bigger_child]=temp;
            }
            root=bigger_child;
        }
    }
}

void print_array(int x[],int size) 
{ 
    int i; 
    for(i=0;i<size;i++) 
        printf("%d\t",x[i+1]); 
    putchar('\n'); 
} 
-----------------------------------------------------------

The approach:
   The approach is simple. This program uses a max heap to sort the numbers in ascending order. You know that in a max heap root is largest element. This program works on the below logic:
1. first take an array from user. Notice that I have saved the elements from index one leaving index 0 empty. See remark1 below.
2. make that array to satisfy max heap property(max - heapify array).
3. Now swap last element of heap with the root. So that the biggest element comes last.
4. Now leave the last element and assume rest array as heap.
5. Adjust the new heap so satisfy max heap property.
6. Repeat step 3-5 till only root is left as rest part of heap.
7. You will be getting an increasingly sorted array.
you can refer to wikipedia for more theory and graphics about heap sort.

Remark:
1. This is the convention I follow when I implement heap on an array because this gets you left and right children as 2*current_node, and (2*current_node + 1). You can use other convention of starting with index 0. In that case left and right children will be evaluated (2*current_node+1), and (2*current_node + 2) which seems a little bigger expressions to me :P

2 comments:

  1. Giving an error with a pop up infinite times even without compiling.
    Access Violation at address 04042FC7 in module 'cp.dll'. Read of address 00000060

    ReplyDelete
  2. In reply of "Giving an error with a pop up infinite times even without compiling...."

    This is problem with your compiler/IDE installation. Please reinstall your compiler and then try again or try with another computer. The code is perfectly fine.

    ReplyDelete

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply