programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

Sorting elements in Ascending Order

Ranch Hand
Posts: 32
• Number of slices to send:
Optional 'thank-you' note:

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?

author
Posts: 23928
142
• 1
• Number of slices to send:
Optional 'thank-you' note:

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

Henry Wong
author
Posts: 23928
142
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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.

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
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??

Marshal
Posts: 76802
366
• Number of slices to send:
Optional 'thank-you' note:
Not sure I understand your question, I am afraid. Please draw a diagram which shows what you expected to happen and what is actually happening.

Anshul Srivastava
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Sorry for the trouble!

Here, I get the values according each iterations:

But in compiler it's show the value of num[j] = 20 when i = 1.

Thanks!