Heapsort is not modular For the time being, I am not interested in any recursive solution. The code is not modularized; the whole body is inside the main function.

#include&lt;iostream.h&gt;
#include&lt;stdlib.h&gt;
/*This code is written by NF between any time of 120711 and 210711;modified at 231211*/

int main()
{
    int length=0,min_heapified=0,start_length=0,index=0;
    cout&lt;&lt;"Enter Array Length:";
    cin&gt;&gt;length;
    int array[length];
    for(int i=0;i&lt;length;i++)
    {
        array[i]=rand()%100;
    }
    cout&lt;&lt;"Array is filled now with random elements";
    cout&lt;&lt;"\nSorted Array:"&lt;&lt;endl;
    do
    {       
            do
            {
                min_heapified=0;
                for(int root=0;root&lt;length/2;root++)/*As the leaf nodes have no child they should not be in a consideration while swapping so looping upto length/2 is enough*/
                {

                    int left=((2*root)+1);
                    int right=((2*root)+2);

                    if(left&lt;length)
                    {
                           if(array[left]&lt;array[root])
                           {
                                int swap=array[left];
                                array[left]=array[root];
                                array[root]=swap;
                                min_heapified=1;    
                           }
                     }
                    if(right&lt;length)
                     {
                            if(array[right]&lt;array[root])
                            {
                                 int swap=array[right];
                                 array[right]=array[root];
                                 array[root]=swap;
                                 min_heapified=1;    
                             }
                      }

                  }

             }while(min_heapified&gt;0);

            int swap=array[0];
            array[0]=array[--length];/*modification done at this point to avoid keeping the sorted elements into another array;and swapping done between the 0th and last element*/
            array[length]=swap;
            cout&lt;&lt;array[length]&lt;&lt;" "; 

 }while(length&gt;0);

  return 0;

}
int length=0,min_heapified=0,start_length=0,index=0;


I prefer one declaration per line because it's easier to read and find the type of variables. Furthermore, min_heapified is only used inside the do-while loop, so declare it there:

do {       
    int min_heapified = 0;
    do {
...




Change 

if(x&lt;=(length-1))


to

if (x &lt; length)


It's more common and simple.



Instead of

int swap=array[left];
array[left]=array[root];
array[root]=swap;


you should create a swap function. Maybe it already exists in a library.

int swap(array, index, rootIndex) {
    int swap = array[index];
    array[index] = array[rootIndex];
    array[root] = swap;
}




If I'm right you could extract the following to a new function:

if(array[left]&lt;array[root])
{
    int swap=array[left];
    array[left]=array[root];
    array[root]=swap;
    min_heapified=1;    
}


It looks for me that the other if do the same. The only difference is the used index, so I'd write something like this:

int swapIfLess(array, index, rootIndex) {
    if (array[index] &lt; array[rootIndex]) {
        swap(array, index, rootIndex)
        return 1;   
    }
    return 0;
}




if (left &lt; length) {
    if (swapIfLess(array, left, root)) {
        min_heapified=1;    
    }
}
if (right &lt; length) {
    if (swapIfLess(array, right, root)) {
        min_heapified=1;    
    }
}




The i variable looks unnecessary:

int root=i;


Use the root variable in the for loop:

for (int root = 0; root &lt; length; root++)


Maybe you should rename it to rootIndex.



Extract more functions. The for loop (inside the do-while loops) looks as a good candidate for this. Additionally, you should have a readInputArray function and a printResults function too.