GeeksforGeeks » Interview Questions
Write a program for followilg statement..
(4 posts)-
In a given array of 1's and 0's arrange them in sorted order.ie 0's followed by 1's..
-
#include<stdio.h> void main() { int a[] = {1,1,0,0,0}; int size = sizeof(a)/sizeof(a[0]); int i = 0; int j = size -1; while(i<j) { while(!a[i] && i < size) i++; while(a[j] && j) j--; if(i<j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } for(i = 0; i < size; i++) { printf("%d ",a[i]); } printf("\n"); } -
it's a modified dutch flag problem
Reply
You must log in to post.