How to suppurate 0's and 1's in C#


I have one array which is having 0's and 1's in it which is un sorted one. I want to suppurate 0'a and 1's within the same array. What I mean is all 0's should come one side and all 1's should come in another side in an array.

For Example I am having a array { 1, 0, 0, 1, 0, 0} and want to sort it like
{ 0, 0, 0, 0, 1, 1 }



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int val = 0;
int[] array = { 1, 0, 0, 1, 0, 0, 1, 1, 0, 0,1,1,1,0,0,0,1,0,1,1,0,0 };
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 0)
{
int temp = array[val];
array[val] = array[i];
array[i] = temp;
val++;
}
}
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]);
}

Console.Read();
}

}
}


Here I checked the condition array element is zero or not, if yes I am assigning the value into array[i].

Happy Programming!!!


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: