Computer Science, asked by supritha1101, 4 months ago

c program to arrange balls of Red ,green and blue​

Answers

Answered by aireneflaga
2

Answer:

Input = [1, 2, 0, 1, 0, 1, 2, 0, 0, 0, 0]

Output = [0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2]

Answered by skprincktr
0

Answer:

#include <stdio.h>

//here red->0, white->1, blue->2

int arr[] = { 0,1,0,2,2,0,1,1,0 };

int arr_size = sizeof(arr)/sizeof(arr[0]);

void swap(int *a, int *b);

void dutchflag( );

int main()

{

int c;

dutchflag();

printf("\ndutch flag Order: ");

for (c = 0; c < arr_size; c++)

printf("%d ", arr[c]);

return 0;

}

/* test function to to Sort the balls in dutch flag order*/

void dutchflag( )

{

int i = 0,j=0 , k = arr_size - 1;

while(j <= k)

{

switch(arr[j])

{

case 0:

swap(&arr[i++], &arr[j++]);

break;

case 1:

j++;

break;

case 2:

swap(&arr[j], &arr[k--]);

break;

}

}

}

/* Function to swap *a and *b */

void swap(int *a, int *b)

{

int temp = *a;

*a = *b;

*b = temp;

}

Similar questions