Tuesday, 8 October 2013

HEAP SORT

 #include<stdio.h>
    void max_heapify(int *,int);
    void build_max_heap(int *,int);
    void heapsort(int *,int);
    void swap(int *,int *);
    int heapsize;
    int main()
    {
            int *arr,n,i;
            printf("Enter no. of elements = ");
            scanf("%d",&n);
            arr=(int *)malloc(sizeof(int)*n);
            for(i=0;i<n;i++)
            {
             printf("Enter array elements = ");
             scanf("%d",&arr[i]);
            }
            //heapsize = n;
            heapsort(arr,n);
            printf("\nAfter heapsort \n");
            for(i=0;i<n;i++)
            {
                printf("%d  ",arr[i]);
            }
        return 0;
    }
 void heapsort(int *arr,int len)
 {
   int i;
   build_max_heap(arr,len);
    for(i= len-1;i>=1;i--)
    {
        swap(&arr[0],&arr[i]);
        heapsize = heapsize -1;
        max_heapify(arr,0);
    }
 }
void max_heapify(int *arr,int i)
{
    int l=2*i,r=2*i+1,largest;
    if(l<heapsize && arr[l]>arr[i])
        largest = l;
    else
        largest = i;
    if(r<heapsize && arr[r]>arr[largest])
        largest = r;

    if(largest != i)
    {
        swap(&arr[i],&arr[largest]);
        max_heapify(arr,largest);
    }
     }
void build_max_heap(int *arr,int len)
{
    heapsize = len;
    int i;
    for(i =len/2;i>=0;i--)
    {
        max_heapify(arr,i);
    }
}
void swap(int *a ,int *b)
{
    int temp = *a;
    *a= *b;
    *b= temp;
}

Program of priority queue using linked list

/* Program of priority queue using linked list*/
# include<stdio.h>
# include<malloc.h>

struct node
{
    int priority;
    int info;
    struct node *link;
}*front = NULL;

main()
{
    int choice;
    while(1)
    {
        printf("1.Insert\n");
        printf("2.Delete\n");
        printf("3.Display\n");
        printf("4.Quit\n");
        printf("Enter your choice : ");
        scanf("%d", &choice);

        switch(choice)
        {
         case 1:
            insert();
            break;
         case 2:
            del();
            break;
         case 3:
            display();
            break;
         case 4:
            exit(1);
         default :
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while*/
}/*End of main()*/

insert()
{
    struct node *tmp,*q;
    int added_item,item_priority;
    tmp = (struct node *)malloc(sizeof(struct node));
    printf("Input the item value to be added in the queue : ");
    scanf("%d",&added_item);
    printf("Enter its priority : ");
    scanf("%d",&item_priority);
    tmp->info = added_item;
    tmp->priority = item_priority;
    /*Queue is empty or item to be added has priority more than first item*/
    if( front == NULL || item_priority < front->priority )
    {
        tmp->link = front;
        front = tmp;
    }
    else
    {
        q = front;
        while( q->link != NULL && q->link->priority <= item_priority )
            q=q->link;
        tmp->link = q->link;
        q->link = tmp;
    }/*End of else*/
}/*End of insert()*/

del()
{
    struct node *tmp;
    if(front == NULL)
        printf("Queue Underflow\n");
    else
    {
        tmp = front;
        printf("Deleted item is %d\n",tmp->info);
        front = front->link;
        free(tmp);
    }
}/*End of del()*/

display()
{
    struct node *ptr;
    ptr = front;
    if(front == NULL)
        printf("Queue is empty\n");
    else
    {    printf("Queue is :\n");
        printf("Priority       Item\n");
        while(ptr != NULL)
        {
            printf("%5d        %5d\n",ptr->priority,ptr->info);
            ptr = ptr->link;
        }
    }/*End of else */
}/*End of display() */