/**
sorting Array with Single Loop
*/
class one
{
public static void main(String[]args)
{
int a[]={1,0,0,0,1,0,1,0};
int j=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==0)
{
a[i]=1;
a[j++]=0;
}
}
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
5 Comments
Excellent......... Awesome...... Great work....Hope u r blog will popular very soon....
ReplyDeleteIt doesn't work for array {1,1,1,1,0,0,0,0,1,1}.
ReplyDeleteWhy do you think .... this program won't work for your example.. anyhow for your reference see the below link
Deletehttp://3.bp.blogspot.com/-PdnliNPhmeA/UT4ODghkh6I/AAAAAAAAAGA/evZ0KXn24bI/s1600/Array.png
#include
Deleteint main(int argc, char **argv)
{
int a[]={1,1,1,1,0,0,0,0,1,1};
int j=0,i;
for(i=0;i<10;i++)
{
if(a[i]==0)
{
a[i]=1;
a[j++]=0;
}
}
for(i=0;i<10;i++)
printf("%d\n",a[i]);
return 0;
}
good one...
ReplyDelete