Plus or minus the number of divisions of the sorted array

description

 

N integers arbitrarily set stored in the array A [1..n], the test write algorithm, all rows in all positive negative front (requirement: positive (negative) relative order number in the sequence number unchanged, the time complexity of the algorithm degree of O (n)).

Entry

A plurality of sets of data, each set of two rows of data, the counted number stored in the first row in the array n, n integers second behavior. When n = 0, the input end.

Export

For each data output line, respectively, as a division sorted array.

Sample input 1 

4
1 2 -1 2
5
-1 -2 1 2 3
0

Sample Output 1

1 2 2 -1
1 2 3 -1 -2
#include <iostream>
using namespace std;


int PNS(int a[],int m)
{
	int i,j;
	int b[m];
	
	for(i=0;i<m;i++)
		b[i]=a[i];
		
	int k=0;
	for(i=0;i<m;i++)
	{
		if(b[i]>0)
		{
			a[k]=b[i];
			k++;
		}
	}
	for(i=0;i<m;i++)
	{
		if(b[i]<0)
		{
			a[k]=b[i];
			k++;
		}
	}
	for(i=0;i<m;i++)
	{
		cout<<a[i];
		if(i!=m-1)cout<<" "; 
	}
	cout<<endl;
	return 0;	
}


int main()
{
	while(1)
	{
		int m;int i,j;
		cin>>m;
		if(0==m) break;
		int a[m];
		for(i=0;i<m;i++)
			cin>>a[i];
		PNS(a,m);
	}
 }

 

Published 100 original articles · won praise 4 · Views 3664

Guess you like

Origin blog.csdn.net/weixin_43673589/article/details/104420844