-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5_MoveAllNegativeToOnseSideOfArray.cpp
110 lines (85 loc) · 1.93 KB
/
5_MoveAllNegativeToOnseSideOfArray.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
void basicway(int [], int);
void twoPointerway(int [], int);
void printarray(int [], int n);
void swapvalues(int *, int *);
int main() {
int arr[100];
int i,j,n,choice;
cout<<"Enter size of array"<<endl;
cin>>n;
cout<<"Enter array values"<<endl;
for(i=0;i<n;i++)
cin>>arr[i];
cout<<"Two ways to do it are: \n"<<endl;
cout<<"1.Basic 0(n) method \n";
cout<<"2.Two Pointer Technique\n";
cout<<"\nEnter your choice (1/2) ?"<<endl;
cin>>choice;
if(choice ==1) basicway(arr,n);
else if(choice ==2) twoPointerway(arr,n);
else{
cout<<"You can only enter 1 or 2 !\n Program Ends here!!";
return 0;
}
cout<<"Final array values after shifting all negative values at left are: \n";
printarray(arr,n);
return 0;
}
void basicway(int arr[], int n){
int i,j=0;
for(i=0;i<n; i++){
if(arr[i]<0){
if(i!=j)
swapvalues(&arr[i], &arr[j]);
j++;
}
}
}
void twoPointerway(int arr[], int n){
int left = 0;
int right = n-1;
while(left<=right){
if(arr[left]<0 && arr[right]<0){
left++;
}
else if(arr[left]>=0 &&arr[right]<0){
swapvalues(&arr[left], &arr[right]);
left++;
right--;
}
else if(arr[left]>=0 && arr[right]>=0){
right--;
}
else{
left++;
right--;
}
}
}
void printarray(int arr[], int n){
for(int i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
void swapvalues(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
/*
OUTPUT SAMPLE
Enter size of array
9
Enter array values
-1 2 -3 4 5 6 -7 8 -9
Two ways to do it are:
1.Basic 0(n) method
2.Two Pointer Technique
Enter your choice (1/2) ?
1
Final array values after shifting all negative values at left are:
-1 -3 -7 -9 5 6 2 8 4
*/