In the first for loop, I count the occurrence of the negative values, and in the second one, I swap them.
I get an out of bound exception unfortunately, which I don't really understand...Is this handmade algorithm right anyway ? Thanks for insights.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
When i==3, you find tab[3] == 1, so you exchange it with tab[tab.length  leftLength + i] = tab[ll7+1] = tab[5] = 1,
so all you do is exchange two "1" values and go on to the next value, leaving a negative value at index 3.
You'd use tab.length  1 if you have something like this:
Here, since you have an expression i+1 inside the loop, the upper bound of the loop variable i needs to be adjusted so your index expression i+1 doesn't run out of bounds, resulting in an ArrayIndexOutOfBoundsException
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
Junilu Lacar wrote:
Here, since you have an expression i+1 inside the loop, the upper bound of the loop variable i needs to be adjusted so your index expression i+1 doesn't run out of bounds, resulting in an ArrayIndexOutOfBoundsException
But the loop termination is i < tab.length1 so it is OK to add 1 to i without going outofbounds.
Fred Kleinschmidt wrote:But the loop termination is i < tab.length1 so it is OK to add 1 to i without going outofbounds.
That's what I said, isn't it?
I wrote:You'd use tab.length  1 if you have something like this: ...
The code I gave after that was meant to show a CORRECT usage. I suppose it would have been clearer if I had written: "Instead of using just tab.length per the normal idiom, you'd need to adjust the loop index upper bound by one, as shown in the code above, so your ..."
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
Here's how I solved this: Think of it like a modified Bubble Sort, except you're not really sorting the elements in any order, just making the negative values "bubble up" to the end of the array. The idea is to iterate through the array and swap a negative number with the next nonnegative number. The most tricky part is finding the index of the next nonnegative number and guarding against referencing an index that is out of bounds.
The alternative algorithm allows you to eliminate one level of nesting inside the loop.
So for tab = { 25, 3, 5, 1, 4, 12, 1, 1, 21, 7, 1 }, here's how it would go:
first iteration, i = 0
skip to next iteration because tab[0] is positive (25)
next iteration, i = 1
skip to next iteration because tab[1] is positive (3)
next iteration, i = 2
skip to next iteration because tab[2] is positive (5)
next iteration, i = 3
don't skip because tab[3] is negative (1)
find the index j greater than i such that tab[j] is positive (4)
that means j == 4
swap tab[3] and tab[4]
now, tab[3] == 4 and tab[4] == 1
next iteration, i = 4
don't skip because tab[4] is negative (1)
find the index j greater than i such that tab[j] is positive (12)
that means j == 5
swap tab[4] and tab[5]
now, tab[4] == 12 and tab[5] == 1
next iteration, i = 5
don't skip because tab[5] is negative (1)
find the index j greater than i such that tab[j] is positive (21)
that means j == 8
swap tab[5] and tab[8]
now, tab[5] == 21 and tab[8] == 1
... and so on
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
According to the pseudo code you gave me, I came up with the following code, which seems to work. It yields 25 3 5 4 12 21 7 1 1 1 1 as expected.
But I'm sure there is a quicker one, I'm open to any comment and suggestions !
Notes:
Line 1  outer loop condition uses (i < arr.length  1) instead of the idiomatic (i < arr.length) because the inner loop index, j, is initialized to (i + 1)  see my previous comment about this
Line 3  inner loop condition includes arr[i] < 0. This causes the inner loop to terminate if arr[i] is positive to begin with or after a positive number is swapped into arr[i]. This eliminates the need to use another ifstatement, a break statement, and a boolean swapped variable.
Line 4  condition for the ifstatement is simplified because the loop condition ensures that arr[i] < 0 inside the inner forloop.
Line 11  this statement is really optional but it saves a few iterations if there are many negative numbers in the array. This terminates the outer loop when no swap happens in the inner loop and arr[i] is still negative. That means the rest of the array after arr[i] does not have any positive numbers.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
