Răspuns :
#include <iostream>
using namespace std;
int main()
{
int sir[1000], n;
// Introduc numarul de elemente ale sirului
cin >> n;
// Introduc elementele sirului
for (int i = 0; i < n; i++)
{
cin >> sir[i];
}
// Impart sirul in doua
int sirPare[1000];
int sirImpare[1000];
int k = 0, l = 0;
for (int i = 0; i < n; i++)
{
if(sir[i] % 2 == 0)
{
sirPare[k] = sir[i];
k++;
int p = k-1;
// Folosesc metoda inserarii de fiecare data cand sirul creste
while (p > 0 && sirPare[p] < sirPare[p - 1])
{
int aux = sirPare[p];
sirPare[p] = sirPare[p - 1];
sirPare[p - 1] = aux;
p--;
}
}
else {
sirImpare[l] = sir[i];
l++;
int p = l-1;
// Folosesc metoda inserarii de fiecare data cand sirul creste
while (p > 0 && sirImpare[p] < sirImpare[p - 1])
{
int aux = sirImpare[p];
sirImpare[p] = sirImpare[p - 1];
sirImpare[p - 1] = aux;
p--;
}
}
}
// Afisez sirul numerelor pare
for (int i = 0; i < k; i++)
{
cout << sirPare[i] << " ";
}
// Afisez sirul numerelor impare
for (int i = 0; i < l; i++)
{
cout << sirImpare[i] << " ";
}
return 0;
}
SOLUTIA MEA (EFICIENTA DIN PDV SPATIU)
#include <iostream>
using namespace std;
int main() {
int v[1001], par = -1, imp = 0, n, x;
cin >> n;
imp = n;
for (int i = 0; i < n; i++) {
cin >> x;
if (x % 2 == 0)
v[++par] = x;
else
v[--imp] = x;
}
for (int i = 0; i < par; i++)
for (int j = i + 1; j <= par; j++)
if (v[j] < v[i])
swap(v[i], v[j]);
for (int i = imp; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (v[j] < v[i])
swap(v[i], v[j]);
for (int i = 0; i < n; i++)
cout << v[i] << " ";
return 0;
}
SOLUTIA LUI RAYZEN, OPTIMIZATA
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, a[1001], b[1001], n1 = 0, n2 = 0, x;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x % 2 == 0)
a[n1] = x, n1++;
else
b[n2] = x, n2++;
}
sort(a, a + n1);
sort(b, b + n2);
for (int i = 0; i < n1; i++)
cout << a[i] << ' ';
for (int i = 0; i < n2; i++)
cout << b[i] << ' ';
return 0;
}