include include include using namespace std int main clrs

# include <iostream>

# include <conio.h>

#include<stdlib.h>

using namespace std;

int main()

{

//clrscr();

int a[50],size;

int p,c;

cout<<\"Enter the Size of an Array :\";

cin>>size;

cout<<\"Enter the elements:\";

//cin>>a[1];

for(int i=1;i<=size;i++)

{

   // cout<<\"Enter the elments :\";

    cin>>a[i];

    p=i/2;

    c=i;

    while(1)

    {

    if( a[c] > a[p])

    {

        int t=a[c];

        a[c]=a[p];

        a[p]=t;

    }

    c=p;

    p=p/2;

    if(p<1)

    {

        break;

    }

    }

}

cout<<endl<<\"Elements after MaxhHeap are ...\"<<endl;

for(int i=1;i<=size;i++)

{

    cout<<endl;

    cout/*<<\"Arr[\"<<i<<\"] :\"*/<<a[i];

}

int j=size;

int lc,rc;

while(j>1)

{

     if(a[1] > a[j])

     {

    int t=a[1];

    a[1]=a[j];

    a[j]=t;

    j--;

     }

     else

     {

    j--;

    continue;

     }

     p=1;

     while(p < j)

     {

     lc=p*2;

     rc=p*2 + 1;

     if(lc>=j || rc >=j)

     {

            break;

     }

if(a[p] < a[lc] && a[lc] > a[rc])

{

    int temp=a[lc];

    a[lc]=a[p];

    a[p]=temp;

    p=lc;

     }

     else if (a[p] < a[rc] && a[rc] > a[lc])

     {

    int temp=a[rc];

    a[rc]=a[p];

    a[p]=temp;

    p=rc;

     }

     else

     {

      break;

     }

     }

}

cout<<endl<<\"Elements after Maxheap Sorted are ...\"<<endl;

for(int i=1;i<=size;i++)

{

    cout<<endl;

    cout/*<<\"Arr[\"<<i<<\"] :\"*/<<a[i];

}

getch();

}

Note: The above C++ program is performing the follwoing function:

1. It can accept the number of array size from the user

2. It can build max heap

3. it can sort the array in ascending order.

So, In the above C++ code I need to change the ascending order in to descending order. ( just only no. 3, change the code of ascending order into descending order. for example, when the user enter array size 5, and the elemnts are such as 2,3,4,5,6 then tha max heap will be 6,5,3,2,4. And final the sorting should be Descending order: 6,5,4,3,2.

Solution

if(lc>=j || rc >=j)

     {

            break;

     }

if(a[p] > a[lc] && a[lc] < a[rc])

{

    int temp=a[lc];

    a[lc]=a[p];

    a[p]=temp;

    p=lc;

     }

     else if (a[p] < a[lc] && a[lc] > a[rc])

     {

    int temp=a[rc];

    a[rc]=a[p];

    a[p]=temp;

    p=rc;

     }

# include <iostream> # include <conio.h> #include<stdlib.h> using namespace std; int main() { //clrscr(); int a[50],size; int p,c; cout<<
# include <iostream> # include <conio.h> #include<stdlib.h> using namespace std; int main() { //clrscr(); int a[50],size; int p,c; cout<<
# include <iostream> # include <conio.h> #include<stdlib.h> using namespace std; int main() { //clrscr(); int a[50],size; int p,c; cout<<
# include <iostream> # include <conio.h> #include<stdlib.h> using namespace std; int main() { //clrscr(); int a[50],size; int p,c; cout<<

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site