Package test;
/**
* Implement the moving algorithm before and after the positive and negative elements of the array (the division line is 0 and can be any other number)
* Move a negative number forward to the front of the array, and place a positive number behind it.
* @ Author hao
*
*/
Public class MoveNumTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
Int [] nums = {342,6, 3453645,-324, 1, 6,-4,645,-3534, 34,-3,345,989 56,-6 };
MoveNumTest test = new MoveNumTest ();
Test. movenum1 (nums );
Test. movenum2 (nums );
}
Private void movenum2 (int [] nums ){
Int left = 0;
Int right = nums. length-1;
While (left <right ){
Int tmpLeft = 0;
For (; left <right; left ++ ){
If (nums [left]> 0 ){
TmpLeft = nums [left];
Break;
}
}
If (tmpLeft! = 0 ){
For (; right> left; right --){
If (nums [right] <0 ){
Nums [left] = nums [right];
Nums [right] = tmpLeft;
Break;
}
}
}
}
System. out. println ("\ nleft =" + left + ", right =" + right );
For (int I: nums ){
System. out. print (I + "");
}
}
Private void movenum1 (int [] nums ){
Int left = 0;
Int right = nums. length-1;
While (left <right ){
Int tmpLeft = 0;
For (; left <right; left ++ ){
If (nums [left]> 0 ){
TmpLeft = nums [left];
Break;
}
}
Int tmpRight = 0;
For (; right> left; right --){
If (nums [right] <0 ){
TmpRight = nums [right];
Break;
}
}
If (tmpLeft! = 0 & tmpRight! = 0 ){
Nums [left] = tmpRight;
Nums [right] = tmpLeft;
}
}
System. out. println ("left =" + left + ", right =" + right );
For (int I: nums ){
System. out. print (I + "");
}
}
}