Home > Back-end >  This not, hope the great god teach me
This not, hope the great god teach me

Time:10-29

This is a bubble sort in Java, I=0, j=0, is why

CodePudding user response:

Are you sure this is a bubble sort?
Are you sure this code to compile and run normally?
Are you sure array with four Numbers, sort out, row seven digits?

CodePudding user response:

J=0, said the first element from an array starting to adjacent two Numbers, the number will move in turn back, but only cycle time enough? Such as 3, 2, 1, j into 2,1,3 cycle is over, did not achieve the purpose of sorting, clearly need to do it again j loop, a second j loop ends, into 1, 2, 3, sorted, so this I used to control the need to do a few times j cycle, how many times to repeat j loop? Case number 3, 2 times a loop line will be done, is obviously circular array length 1 time, so I from 0 to circular array length - 1

CodePudding user response:

In parentheses for loop, three code,
The first paragraph, it is the initialization code, to do this at the beginning of the cycle, and executed only once, also is the int I=0 and int j=0; Initialization here means to define a variable I, assign a value of 0
The second paragraph, is the judgment condition, is when I & lt; XXX, only this condition was established, the cycle to continue to cycle
The third paragraph, it is after each cycle, execution, i++ here, which means that every cycle, since I add 1,

That is to say, the initialization I=0, because 0 & lt; The length of the array, so the loop condition was established, began circulating
After the first cycle, perform i++, so this time I=1, the array length is 4 here, so 1 & lt; 4 judgment, continue to cycle
And then there are the repeat!
Until I=4, I & lt; 4, end of the loop!
  • Related