Friday 10 November 2017

C Program to Segregate 0s on Left Side & 1s on right side of the Array


Code:

#include

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

    while (left < right)
    {
        /* Increment left index while we see 0 at left */
        while (array[left] == 0 && left < right)
            left++;
        /* Decrement right index while we see 1 at right */
        while (array[right] == 1 && left < right)
            right--;
        /* If left is smaller than right then there is a 1 at left and a 0 at right.  Exchange it */
        if (left < right)
        {
            array[left] = 0;
            array[right] = 1;
            left++;
            right--;
        }
    }
}

int main()
{
    int arr[] = {0, 1, 0, 1, 1, 0};
    int array_size = 6, i = 0;

    segregate0and1(arr, array_size);
    printf("segregated array is ");
    for (i = 0; i < 6; i++)
        printf("%d ", arr[i]);
    getchar();
    return 0;
}

Output:

segregated array is 0 0 0 1 1 1

More C Problems









100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...