-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathseparacao.cpp
More file actions
52 lines (40 loc) · 827 Bytes
/
separacao.cpp
File metadata and controls
52 lines (40 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
Este programa separa todos negativos dos positivos em um array.
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
/*
Separa um array colocando todos os negativos antes de j e os positivos depois d de j
Mas essa função não garante que todos os elementos após e antes de j estejam ordenados.
*/
void sep(vi &a, int b, int e, int *j)
{
*j = b;
for (int i = b; i <= e; ++i)
{
if (a[i] < 0)
{
swap(a[*j], a[i]);
(*j)++;
}
}
}
int main()
{
int n, j;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
}
sep(a, 0, n - 1, &j);
printf("j=%d\n",j);
for (int i = 0; i < n; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}