# Help me understand the program! pls

• August 12th, 2013, 03:43 AM
YogiB1810
Help me understand the program! pls
Hi here is the array sort code i came across.

Code java:

```class Arraysorting { public void bubblesort(Int A[]) { // array elements are : {5,3,8,9,2,1,12,90,15}   int i,j,tmp; for (i=0 ; i <10 ; i++) for (j=0; j<9-i-1; j++) { if (A[j] > A[j +1]) { tmp = A[j]; A[j] = A[j +1]; A[j +1] = tmp; } } System.out.println("Array in ascending order is ->"); for (i=0 ; i <10; i++) System.out.println(A[i] + "\n"); } }```

here i couldn't understand the use of " (j=0; j<9-i-1; j++)".

If possible also help me understand the program on the whole.
• August 12th, 2013, 07:06 AM
lil_misfitss
Re: Help me understand the program! pls
Are the array elements in order like is A[0] = 5? I'm trying to help but this might clear it up for me.

edit :
There is an error on (Int A[]){ it should be (int A[]){

--- Update ---

Alright so I ran the program (after making a main method for it) and here was the output:
Array in ascending order is ->
1
2
3
4
5
8
9
12
15
90
Then i had an exception in main with an out of bounds exception.(I don't know how exceptions yet. My fault coding the main method.) But as you can see it puts the numbers in order from least to greatest. I tried to trace the code (see how it worked) and goodness it was hard! Once I get a clear answer on how it works i'll tell you! Hope this helped!
• August 12th, 2013, 11:15 AM
jps
Re: Help me understand the program! pls
As has been mentioned to you in the past, please use code tags when posting code, instructions can be found on the Announcements page. This is to preserve readability of the code for future readers, and to save time for the volunteers who help answer questions.

Quote:

Originally Posted by YogiB1810
here i couldn't understand the use of " (j=0; j<9-i-1; j++)".

Have you tried to step through the code on paper? Start at the first loop, write out what happens in the background as the code progresses. You will easily see which elements of the array are being considered on each iteration of the loop. You will see when the conditional passes and which elements are swapped.

• August 13th, 2013, 02:34 AM
YogiB1810
Re: Help me understand the program! pls
Hi, can you pass me the code you that you have altered. I guess it would be better to understand.

Quote:

Originally Posted by lil_misfitss
Are the array elements in order like is A[0] = 5? I'm trying to help but this might clear it up for me.

edit :
There is an error on (Int A[]){ it should be (int A[]){

--- Update ---

Alright so I ran the program (after making a main method for it) and here was the output:
Array in ascending order is ->
1
2
3
4
5
8
9
12
15
90
Then i had an exception in main with an out of bounds exception.(I don't know how exceptions yet. My fault coding the main method.) But as you can see it puts the numbers in order from least to greatest. I tried to trace the code (see how it worked) and goodness it was hard! Once I get a clear answer on how it works i'll tell you! Hope this helped!

• August 13th, 2013, 03:13 AM
syedbhai
Re: Help me understand the program! pls
Hello.
Did you copy the code from internet?
If so then I would suggest you should first learn about bubble sort and write your own code. Initially it may look difficult.
But as you step in it shall become easier.

Syed.
• August 14th, 2013, 07:50 AM
YogiB1810
Re: Help me understand the program! pls
Hi Syed,

No, it was from one of the books i was referring to and i came across this program.