top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the number of elements that can be sorted in O(logn) time using heap sort?

0 votes
297 views
What is the number of elements that can be sorted in O(logn) time using heap sort?
posted Jun 21, 2017 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+2 votes

How to sort elements in a matrix that are above the principal diagonal using C/C++, diagonal elements are also included?

+2 votes
#include <stdio.h>
#include <stdlib.h>

void makeHeap(int heap[],int,int);
int delete(int heap[]);
int no;

int main()
{
    int heap[10],i,num,temp[10],n;

    printf("Enter the number\n");
    scanf("%d",&no);

    n=no;
    for(i=0;i<no;i++)
    {
        scanf("%d",&num);
        makeHeap(heap,num,i);
    }
    printf("The heap element are\n");

    for(i=0;i<no;i++)
        printf("%d\t",heap[i]);

    for(i=n-1;i>=1;i--)
       temp[i]=delete(heap);

    printf("\nThe sorted elements are\n");

    for(i=0;i<n;i++)
       printf("%d\t\t",temp[i]);

    return 0;
}
void makeHeap(int heap[],int data,int index)
{
    int parent,temp;
    heap[index]=data;
    while(index!=0)
    {
        parent=(index-1)/2;
        if(heap[parent]>heap[index])
        {
            temp=heap[parent];
            heap[parent]=heap[index];
            heap[index]=temp;
        }

        index=parent;

    }

}

int delete(int heap[])
{
    int i,left,min,m,c;
    int temp;
    int value=heap[0];

    heap[0]=heap[no-1];
    no--;
    i=0;

    //Problem is  coming in this while loop please help 
    while(i<no)
    {
        left=2*i+1;

        if(heap[left]<heap[i]&&left<=no)
        {
            min=left;
            temp=heap[left];
            heap[left]=heap[i];
            heap[i]=temp;

        }
        else
           min=i;

        if((heap[left+1]<heap[i])&&((left+1)<=no))
        {
           min=left+1;
            temp=heap[left+1];
            heap[left+1]=heap[i];
            heap[i]=temp;
        }
        i=min;
    }

    return value;
}
...