GeeksforGeeks » Interview Questions

Write a program for followilg statement..

(4 posts)

Tags:

  1. vivekpandian
    Member
    Posted 6 months ago #

    In a given array of 1's and 0's arrange them in sorted order.ie 0's followed by 1's..

  2. kartik
    Moderator
    Posted 6 months ago #

  3. vasu
    guest
    Posted 6 months ago #

    #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");
    }
    
  4. vasu
    guest
    Posted 6 months ago #

    it's a modified dutch flag problem


Reply

You must log in to post.

RSS feed for this topic