java - Alternative positive, negative number in an array -


Looking at an array of positive and negative numbers (no zero), I have to arrange them in such a way that the positive And negative numbers should be arranged in a sequential manner. The number of positive and negative numbers can not be equal, if there is no positive number (or negative), then the remaining negative numbers (or positive) are added to the end of the array. The command is important, the IIF input array is {2, -1, -3, -7, -8, 9, 5, -5, -7}, then the output array must be {2, -9, -3, 5, - 7, -8, -5, -7}. Code is used in O (N) without using any other array.

Here's my solution in Java, I test it again and this works, however, I'm not sure that this run time in O (n) basically, I first positive And count the number of negative numbers. So I have two indices I = 0 and j = 1. I is always 1 step forward. From there, I check that the number in me is positive and that is negative, if it is not, then through the array again Until I find the next positive / negative for the right position and take it to the right position. / P>

Any suggestions are highly appreciated thanks!

  // Arrange negative and positive, arrange them so that positive numbers are negative / if no position or neg is not according to the left number, then the remainder must be added to the array. // should be O (n) no additional array public constant zero change (int [] a) {int pos = 0; Int neg = 0; Int index = 0; While (C & LT; Aline) {if (a [index]> 0) {pos ++; } And Neg ++; Index ++; } Int i = 0; Int j = 1; Int temp = 0; // until (pause> 0 & amp; amp; and & gt; 0) {// if (a [i]> gt; {pos--; If (a [j] <0) {i + = 2; J + = 2; Neg--; } And / a [J]> gt; 0 {While (A [J]> 0) {J ++; } // a [j] & lt; 0 negative -; // Transfer that number to the appropriate place while (j & gt; i) {temp = a [j]; One [j] = one [ja -1]; A [j -1] = temporary; J--; } // end while I + = 2; J + = 2; }} And / a [i] & lt; 0 {While (a [i] <0) {i ++; } // a [i] & gt; 0 // Move that number to the appropriate location while (i> (J-1)) {temp = a [i]; A [i] = a [i-1]; A [i-1] = temporary; I--; }} // end else}}    

However, if I am not It is running in o (n) time

so I do not think it runs in O . The moment you find the "next right element and move it to the right place", you should

  1. loop over the remaining array. Which means that for the worst position (initially with all positive elements array, and finally all negative elements) you will loop for every positive element and once more than half of the "unbranded" part of the array, / Li>
  2. When you move the element back to the correct position, you move it from the position, which means that you will once again loop on the "unbroken" section of the array < / Ol>

    actually Does not yet sure how you O (n) to run If you need to recognize the need to be respected in order without using third array it.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -