forked from MainakRepositor/500-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
105.cpp
87 lines (78 loc) · 1.66 KB
/
105.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// A C++ program to put positive
// numbers at even indexes (0, 2, 4,..)
// and negative numbers at odd
// indexes (1, 3, 5, ..)
#include <iostream>
using namespace std;
class GFG
{
public:
void rearrange(int [],int);
void swap(int *,int *);
void printArray(int [],int);
};
// The main function that rearranges
// elements of given array. It puts
// positive elements at even indexes
// (0, 2, ..) and negative numbers
// at odd indexes (1, 3, ..).
void GFG :: rearrange(int arr[], int n)
{
// The following few lines are
// similar to partition process
// of QuickSort. The idea is to
// consider 0 as pivot and
// divide the array around it.
int i = -1;
for (int j = 0; j < n; j++)
{
if (arr[j] < 0)
{
i++;
swap(&arr[i], &arr[j]);
}
}
// Now all positive numbers are at
// end and negative numbers at the
// beginning of array. Initialize
// indexes for starting point of
// positive and negative numbers
// to be swapped
int pos = i + 1, neg = 0;
// Increment the negative index by
// 2 and positive index by 1,
// i.e., swap every alternate negative
// number with next positive number
while (pos < n && neg < pos &&
arr[neg] < 0)
{
swap(&arr[neg], &arr[pos]);
pos++;
neg += 2;
}
}
// A utility function
// to swap two elements
void GFG :: swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// A utility function to print an array
void GFG :: printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Driver Code
int main()
{
int arr[] = {-1, 2, -3, 4,
5, 6, -7, 8, 9};
int n = sizeof(arr) / sizeof(arr[0]);
GFG test;
test.rearrange(arr, n);
test.printArray(arr, n);
return 0;
}