Cyran Meriel

Ranch Hand

Posts: 32

posted 2 years ago

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?

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?

posted 2 years ago

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

- 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

posted 2 years ago

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

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

Cyran Meriel

Ranch Hand

Posts: 32

posted 2 years ago

Thanks! I understand the code now. Yeah I'm not used to the java logic, but it's starting to sink in now.

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.

Anshul Srivastava

Greenhorn

Posts: 16

posted 1 year ago

I'm just getting stucked at above program when outer loop i = 1 then inner loop j count starts indexing with j = i + 1; i.e j = 1 + 1 = 2; and that counts on array on 10 which has index no. on 2, that means 50 > 10 ( num[i] = 50) but in program the iteration shows that as 50 > 20! How??

Campbell Ritchie

Marshal

Posts: 55761

163

Consider Paul's rocket mass heater. |