-
Notifications
You must be signed in to change notification settings - Fork 1
/
Arranging_The_Array.cpp
43 lines (43 loc) · 1.17 KB
/
Arranging_The_Array.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
class Solution
{
public:
void merge_sort(int arr[], int low, int mid, int high){
int i = low, j = mid+1;
vector<int> cur;
while(i<=mid && j<=high){
if(arr[i] < 0 || (arr[i]>=0 && arr[j]>=0)){
cur.push_back(arr[i]);
i++;
}
else{
cur.push_back(arr[j]);
j++;
}
}
while(i<=mid){
cur.push_back(arr[i]);
i++;
}
while(j<=high){
cur.push_back(arr[j]);
j++;
}
for(int k = low; k<=high; k++){
arr[k] = cur[k-low];
}
}
void merge(int arr[], int low, int high){
if(low<high){
int mid = (high+low)/2;
merge(arr,low,mid);
merge(arr,mid+1,high);
merge_sort(arr,low,mid,high);
}
}
void Rearrange(int arr[], int n)
{
// Your code goes here
int low = 0, high = n-1;
merge(arr,low,high);
}
};