forked from AkhzarFarhan/HactoberFest-2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.cpp
More file actions
44 lines (35 loc) · 985 Bytes
/
sorting.cpp
File metadata and controls
44 lines (35 loc) · 985 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
// C++ program to rearrange positive and negative
// numbers
#include <bits/stdc++.h>
using namespace std;
void rearrange(int a[], int size)
{
int positive = 0, negative = 1;
while (true) {
/* Move forward the positive pointer till
negative number number not encountered */
while (positive < size && a[positive] >= 0)
positive += 2;
/* Move forward the negative pointer till
positive number number not encountered */
while (negative < size && a[negative] <= 0)
negative += 2;
// Swap array elements to fix their position.
if (positive < size && negative < size)
swap(a[positive], a[negative]);
/* Break from the while loop when any index
exceeds the size of the array */
else
break;
}
}
// Driver code
int main()
{
int arr[] = { 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 };
int n = (sizeof(arr) / sizeof(arr[0]));
rearrange(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}