Microsoft Interview Question
Software Engineer / DevelopersSimple. Check this coe.
int a[]={1,1,0,0,1,1,1,0,1,0};
int length=sizeof(a)/sizeof(a[0]);
for(i=0;i<length;i++)
{
//Important to understand this
count[a[i]]++;
}
for(i=0;i<count[0];i++)
a[i]=0;
for(j=count[0];j<count[0]+count[1];j++)
a[j]=1;
//printf("Count %d %d\n",count[0],count[1]);
for(i=0;i<length;i++)
printf("%d ",a[i]);
function partition(array, left, right, pivotIndex)
pivotValue := array[pivotIndex]
swap array[pivotIndex] and array[right] // Move pivot to end
storeIndex := left
for i from left to right - 1 // left ≤ i < right
if array[i] ≤ pivotValue
swap array[i] and array[storeIndex]
storeIndex := storeIndex + 1
swap array[storeIndex] and array[right] // Move pivot to its final place
return storeIndex
Reference: From wiki
Following algorithm has time complexity O(n).
It can used for above problem with pivote element as 1.
But, this algorithm is not stable.
Hop, question does not asking stable algorithm!
Rachit got the best answer -- short and sweet. Lemme encode it as a function in Python:
def sort01(array):
s = sum(array)
return (len(array)-s)*[0] + s*[1]
Hi all, sometimes you are asked to do this in one pass, without any other data structures. I think this is to avoid the solution of just summing the array and then creating a new array.
This is my answer in python. I am sure there are ways to make this better:
from array import array
a = array('i',[1,0,0,1,0,1,1,0,1,1,0,1])
ones = 0
sum = 0 ;
for i in range(0,len(a)):
if a[i] == 1:
ones += 1
if a[i] == 0 & (ones > 0):
a[i] = 1
a[i-ones] = 0
int[] array = new int[] { 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1 };
int zerocount = 0;
int onescount = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 0)
{
if (i == 0)
{
zerocount += 1;
continue;
}
}
if (i > zerocount && array[i] == 0)
{
int temp = array[i - onescount];
array[i - onescount] = array[i];
array[i] = temp;
zerocount += 1;
}
else if (array[i] == 1)
{
onescount += 1;
}
}
public static void main(String[] args) {
int arr[]= {1,1,1,0,1,0,0,1,0};
int x=0;
System.out.println("Hello JAved");
for(int i=1; i<arr.length;)
{
if(arr[i]<arr[i-1])
{
x=arr[i];
arr[i]= arr[i-1];
arr[i-1]=x;
if(arr[1]==1) // preventing getting i value as negative
i=1;
else
i--;
}
else
i++;
}
for(int i=0;i<=arr.length-1;i++){
System.out.print(" "+arr[i]+" ");
}
}
public static void main(String[] args) {
int arr[]= {1,1,1,0,1,0,0,1,0};
int x=0;
System.out.println("Hello JAved");
for(int i=1; i<arr.length;)
{
if(arr[i]<arr[i-1])
{
x=arr[i];
arr[i]= arr[i-1];
arr[i-1]=x;
if(arr[1]==1) // preventing getting i value as negative
i=1;
else
i--;
}
else
i++;
}
for(int i=0;i<=arr.length-1;i++){
System.out.print(" "+arr[i]+" ");
}
}
The most efficient way to sort the array of 0s and 1s is to count the total number of 0s present in the array and to count the total number of 1s in the array and then traversing the array from 0 indexes to the count index insert 0 in the array and then next index to the end index insert 1 in the array and hence the array is sorted.
Implementation:
void sort-array(int arr[], int n){
int count = 0;
for(int i = 0; i < n; i++)
if(arr[i] == 1)
count++;
for(int i = 0; i < temp; i++){
arr[i] = 0;
for(int i = temp; i < (n - temp); i++)
arr[i] = 1;
}
i = 0;
- topcoder December 26, 2009j = n-1;
while( i < j){
while( array[i] == 0 && i < j )
i++;
while( array[j] == 1 && j > i )
j--;
if ( i < j )
swap( a[i], a[j] );
}
This is O(n) solution