#include<iostream>
using namespace std;
void sort(int a[],int n); // call by reference is used
int max(int a[],int n); // call by reference is used
int main()
{
int n;
cout << "Enter size of array" << endl;
cin >> n;
int a[n]; // Dynamic Initialization of array
for(int i=0;i<n;i++)
cin >> a[i];
sort(a,n);
cout << "Sorted array:" << endl;
for(int i=0;i<n;i++)
cout << a[i] << endl;
return 0;
}
void sort (int a[],int n)
{
int temp,ind;
for(int i=n-1;i>0;i--)
{
ind=max(a,i);
temp=a[ind];
a[ind]=a[i];
a[i]=temp;
}
}
int max(int a[],int n)
{
int max=0,ind;
for(int i=0;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
ind=i;
}
}
return ind;
}
using namespace std;
void sort(int a[],int n); // call by reference is used
int max(int a[],int n); // call by reference is used
int main()
{
int n;
cout << "Enter size of array" << endl;
cin >> n;
int a[n]; // Dynamic Initialization of array
for(int i=0;i<n;i++)
cin >> a[i];
sort(a,n);
cout << "Sorted array:" << endl;
for(int i=0;i<n;i++)
cout << a[i] << endl;
return 0;
}
void sort (int a[],int n)
{
int temp,ind;
for(int i=n-1;i>0;i--)
{
ind=max(a,i);
temp=a[ind];
a[ind]=a[i];
a[i]=temp;
}
}
int max(int a[],int n)
{
int max=0,ind;
for(int i=0;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
ind=i;
}
}
return ind;
}
No comments:
Post a Comment