Algorithm--Two Baidu pen questions
Today saw a garden friend wrote a blog about the interview of Baidu, became a commentary on the headlines, and then looked at a bit, very interested, the blogger's algorithm ability as I need to improve, estimated his skill is still under me, so again next, here to put their own source paste out.
Baidu Noodles Question (a): Suppose that an integer array has a number of positive and negative numbers, now through an algorithm to make all negative numbers of the array to the left of positive numbers, and to ensure that the relative position of negative and positive elements are unchanged. Space-time complexity requirements were:O (n) and O (1).
actually at the beginning of time I was also confused, after drawing on the paper found that, in fact, is a deformation of the insertion sort. Fortunately, there is no need to compare the size, to compare the size of the time complexity is O (N2), only to judge the positive and negative, the time complexity of just O (n).
such as: -3,1,2,-1,-3,4. In fact, the first negative number behind the positive number (such as-1) is inserted in front of the first positive number (1), each positive number between negative numbers (a) to move a bit, it is so simple. The complexity of the time is just O (n), and the control complexity is O (1).
Algorithm separates positive and negative numbers
void Insertsort (int*a, int size)
int minus=-1,plus=-1;
int tmp=0;
for (int i=0;i<size;i++)
{
if (minus==-1)
{
if (a[i]<0&& plus>=0)
{
Minus=i;
}
if (a[i]>0&& plus<0)
{
Plus=i;
}
}
if (minus>=0&& plus>=0)
{
Tmp=a[plus];
a[plus++] = A[minus];
{
A[K] = a[k-1];
}
a[plus]=tmp;
Minus=-1;
}
}
}
Baidu Noodle Question (b), given a positive array, rearrange the array so that the left of the array is odd, the right is even, and guaranteed that the relative position of the odd and even elements is unchanged. Space-time complexity requirements were: O (n) and O (1).
In fact, there is no difference between the two questions, to say that there is a difference is to see if you understand.
Algorithms separate odd even numbers
void InsertSort1 (int*a, int size)
int minus=-1,plus=-1;
int tmp=0;
for (int i=0;i<size;i++)
{
if (minus==-1)
{
if (a[i]%2!=0&& plus>=0)
{
Minus=i;
}
if (a[i]%2==0&& plus<0)
{
Plus=i;
}
}
if (minus>=0&& plus>=0)
{
Tmp=a[plus];
a[plus++] = A[minus];
{
A[K] = a[k-1];
}
a[plus]=tmp;
Minus=-1;
}
}
}
The test code is as follows:
Test code
int main () {
int a[]={2,1,-2,12,4,-2,-4,-6};
int size=sizeof (A)/sizeof (int);
cout<< "-----------------------algorithm separate positive and negative numbers-----------------------" <<endl;
for (int i=0;i<size;i++)
{
cout<<a[i]<< "\ t";
}
cout<<endl;
Insertsort (a,size);
for (int i=0;i<size;i++)
{
cout<<a[i]<< "\ t";
}
cout<<endl;
cout<<endl;
cout<< "-----------------------algorithm separates odd even-----------------------" <<endl;
int b[]={2,1,-2,12,4,-2,-4,-6};
int s=sizeof (B)/sizeof (int);
for (int i=0;i<s;i++)
{
cout<<b[i]<< "\ t";
}
cout<<endl;
InsertSort1 (b,s);
for (int i=0;i<s;i++)
{
cout<<b[i]<< "\ t";
}
GetChar ();
Return0;
Run structure:
Algorithm--Two Baidu pen questions