Thursday, April 12, 2018

Sort an Array which contains only 0 and 1

void Sort(int arr[], int n)
{
    int count = 0; // Counts the no of zeros in arr

    for (int i = 0; i < n; i++) {
        if (arr[i] == 0)
            count++;
    }

    // Loop fills the arr with 0 until count
    for (int i = 0; i < count; i++)
        arr[i] = 0;

    // Loop fills remaining arr space with 1
    for (int i = count; i < n; i++)
        arr[i] = 1;
}

Second Method

i=0;
n=5;
while(i==n)
{

if(i==n)
 //interchange;

i++;
n--;
}

No comments:

Followers

Link