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 / rajasekaran
#include <stdio.h>
int main() {
int a[8] = {1,0,1,0,1,0,0,1};
int i = 0,j=0;
int sorted = 1;
for(i=0;i<8;i++) {
if (a[i]) continue;
/* Find the nearest one and swap */
for(j=i+1;j<8;j++) {
if (a[j]) {
a[j] = a[i] + a[j];
a[i] = a[j] - a[i];
a[j] = a[j] - a[i];
sorted = 0;
break;
}
}
if (sorted) { break;}
}
printf("\nSorted Array is { ");
for (i=0;i<8;i++) { printf("%d,",a[i]); }
printf("}\n");
}
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
What are enumerated types?
Explain what is the difference between the expression '++a' and 'a++'?
What does sizeof int return?
Where local variables are stored in c?
What is the full form of getch?
Explain how can I read and write comma-delimited text?
What is character constants?
What would the following code segment printint k = 8;docout << "k = " << k << " ";while k++ < 5; a) 13 b) 5 c) 8 d) pointers
What 'lex' does?
What are file streams?
What was noalias and what ever happened to it?
write a program to find out prime number using sieve case?
how to write a c program to print list of fruits in alpabetical order?
What is the purpose of 'register' keyword in c language?
What are the uses of a pointer?