Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Sort_Array 0 1.cpp #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Sort_Array 0 1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// C++ program to sort a
// binary array in one pass
#include <bits/stdc++.h>
using namespace std;

/*Function to put all 0s on
left and all 1s on right*/
void segregate0and1(int arr[],
int size)
{
int type0 = 0;
int type1 = size - 1;

while(type0 < type1)
{
if(arr[type0] == 1)
{
swap(arr[type0],
arr[type1]);
type1--;
}
else
type0++;
}
}

// Driver Code
int main()
{
int arr[] = {0, 1, 0, 1, 1, 1};
int i, arr_size = sizeof(arr) /
sizeof(arr[0]);

segregate0and1(arr, arr_size);

cout << "Array after segregation is ";
for (i = 0; i < arr_size; i++)
cout << arr[i] << " ";

return 0;
}