I understand the basics of this code but I cant figure out what to do when the first number which is 20 finds several numbers that it is larger than. If I understand the code right when num[i] > num[j] it sets num[j] to 0. So num[j] gets moved back in order and is the new starting number. If there was only one number that i was larger than , it would be easy to understand the problem. That lesser number gets [0] position and is the new starting number and the previous starting number becomes num[1], second number.
But like I said if theres more numbers that are lesser than the first number how do the program sort between those numbers?.. 20 is larger than 10, 17, and 5. so 10,17 and 5 gets num[0] but then what? its not in the right order and I cant see where in the program it sorts between those numbers.
Also something that bothers me, I played around with the code and the part "num[j] = t;" if i change the order to "t = num[j];" i get wrong numbers in the print. I mean its exactly the same code but just swapped, how can that effect the outcome of the program?
 1
Cyran Meriel wrote:
But like I said if theres more numbers that are lesser than the first number how do the program sort between those numbers?.. 20 is larger than 10, 17, and 5. so 10,17 and 5 gets num[0] but then what? its not in the right order and I cant see where in the program it sorts between those numbers.
In a bubble sort, only one number is guaranteed to be in place with each pass of the inner loop. So, with this example...
20 is larger than 10. So, 10 gets moved to the zeroth position. And 20 gets moved to where the 10 was located. The two numbers are swapped.
10 is not larger than 17. So, nothing happens... notice that I said 10, and not 20, as that is the new number in the zeroth position.
10 is larger than 5. So, 5 gets moved to the zeroth position. And 10 gets moved to where the 5 was located. The two numbers are swapped.
This completes the inner loop. And one iteration of the outer loop. The result is that the smallest number is at the zeroth position. The next iteration of the outer loop runs, with the "i" variable on the first position (and of course, all comparison and swapping will be with the first position).
Henry
Cyran Meriel wrote:
Also something that bothers me, I played around with the code and the part "num[j] = t;" if i change the order to "t = num[j];" i get wrong numbers in the print. I mean its exactly the same code but just swapped, how can that effect the outcome of the program?
Can you explain how the heck are the two expressions the same?
These are two different variables, "num[j]" and "t". With the first expression, "num[j] = t", both variables now hold the value of t. With the second expression, "t = num[j]", both variables now hold the value "num[j]".
Remember that this isn't algebra (where it is symmetrical)  it is an operation, where the value on the right hand side of the expression is assigned to the variable on the left hand side of the expression.
Henry
Henry Wong wrote:
Remember that this isn't algebra (where it is symmetrical)  it is an operation, where the value on the right hand side of the expression is assigned to the variable on the left hand side of the expression.
Henry
Thanks! I understand the code now. Yeah I'm not used to the java logic, but it's starting to sink in now.
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
