forked from Andyy-18/CPP-BASICS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.cpp
60 lines (48 loc) · 1.17 KB
/
Sort.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
#include<bits/stdc++.h>
using namespace std;
void printarray(int arr[],int n){
for(int i = 0; i<n ; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void sortone(int arr[],int n){
int left = 0;
int right = n-1;
// int step = 0;
while(left < right){
// cout<<"step"<<step++<<endl;
// printarray(arr,n);
while(arr[left] == 0 && left < right){
left++;
}
while(arr[right] == 1 && left < right){
right--;
}
// agar yeha pohoch gye ho,matlab
// arr[left] == 1 & arr[right] == 0
if( left < right){
swap(arr[left],arr[right]);
left++;
right--;
}
}
// cout<<"returning the sorted array : "<<endl;
// printarray(arr,8);
}
int main()
{
int n;
cout<<"Enter the size of array : "<<endl;
cin>>n;
int arr[100];
cout<<"Enter an array : \n";
for(int i=0; i<n; i++){
cin>>arr[i];
}
// int arr[8] = {1,1,0,0,0,0,1,0};
sortone(arr,n);
// cout<<"printing after function call : "<<endl;
printarray(arr,n);
return 0;
}