Take an array of integers containing negative numbers, positive numbers and zeros. Group it with one iteration and in place such that all of the negative numbers come first, followed by all of the zeros, followed by all of the positive numbers.
Input: 5, 3, 0, -6, 2, 0, 5 Output: -6, 0, 0, 3, 2, 5, 5
Note that the numbers do not need to be fully sorted: just sorted by sign.
So, the final array will look like this:
-, -, ..., -, -, 0, 0, ..., 0, 0, +, +, ..., +, +
- You may only use the input array and a constant amount of additional memory (i.e. you may not create any more arrays)
- You may only use one loop, which may execute only as many times as the length of the array. You may not use built-in functions which conceal any kind of loop. This includes built-in sort functions.
- The result should be in the format I described
The winner will be the person who will submit the shortest code (counted in bytes) that changes the initial array into a correct format (like described above).