array contains zeros and ones as elements.we need to bring
zeros one side and one other side in single parse.
ex:a[]={0,0,1,0,1,1,0,0}
o/p={0,0,0,0,0,1,1,1}
Answer Posted / alex r.
Single parse, n/2 steps.
Swapping without 3rd tmp variable.
#include <stdio.h>
int main(void)
{
int i,j;
int stepscount = 0;
int length = 10;
int a[10] = {1,0,0,0,1,0,0,1};
for (i=0,j=length-1;i<j;stepscount++)
{
// dont need move 0 from the start
if (a[i] == 0) i++;
// dont need move 1 from the end
if (a[j] == 1) j--;
// swap 0 and 1
if (a[i] > a[j]) {
a[i] = a[i] ^ a[j];
a[j] = a[j] ^ a[i];
a[i] = a[i] ^ a[j];
i++;
j--;
}
}
for (i = 0; i < length; i++)
{
printf("%d",a[i]);
}
printf("\nSteps: %d\n", stepscount);
return 0;
}
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
How can I invoke another program or command and trap its output?
Explain what is the best way to comment out a section of code that contains comments?
a direct address that identifies a location by means of its displacement from a base address or segment a) absolute address b) relative address c) relative mode d) absolute mode
how to print electricity bill according to following charges first 100 units -1rs per unit for next 200 units-1.50 rs per unit without using conditions
what is stack , heap ,code segment,and data segment
What is a good data structure to use for storing lines of text?
What is the advantage of an array over individual variables?
how could explain about job profile
Why we use void main in c?
What is the difference between far and near ?
What is c basic?
What is #include cctype?
which of the following is not a character constant a) 'thank you' b) 'enter values of p, n ,r' c) '23.56E-o3' d) all of the above
When is the “void” keyword used in a function?
What is %s and %d in c?