I have to solve a problem that consist of sorting a list of number in c++. The condition is :
- the positive, and 0 must be sorted ascending
- negative numbers- descending
if A = {4,-8,7,-6,0,-7,5} the at the final B = {0,4,5,7,-6,-7,-8 }
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int i,j,n,A[100],B[100],A_nenegative[100],A_negative[100],aux,m=0,k=0;
cout<<"max elements"; cin>>n;
cout<<"elements are"<<endl;
for(i=0;i<n;i++)
{
cin>>A[i];
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(A[i]>A[j])
{
aux=A[i];
A[i]=A[j];
A[j]=aux;
}
for(i = 0; i< n; i++)
if(A[i]>=0) {
A_nenegative[i]=A[i];
B[i]=A_nenegative[i];
k++;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(A[i]<A[j])
{
aux=A[i];
A[i]=A[j];
A[j]=aux;
}
for(i=0;i<n;i++)
if(A[i]<0)
{
A_negative[i]=A[i];
m++;
}
}
here is where i stopped. I sort positive numbers in A_nenegative and negative numbers in A_negative. So the question is how can attribute to B first- A_nenegative and then A_negative. I try after sorting positive numbers immediate to attribute to B , something like that:
for(i = 0; i< n; i++)
if(A[i]>=0) {
A_nenegative[i]=A[i];
B[i]=A_nenegative[i];
k++;
}
But i don't know what to do next